Re: Is CArray best for this...
- From: "GT" <ContactGT_remove_@xxxxxxxxxxx>
- Date: Wed, 16 Jan 2008 16:42:09 -0000
"AliR (VC++ MVP)" <AliR@xxxxxxxxxxxxx> wrote in message
news:Ffqjj.33372$4V6.15771@xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Can you post the code where you are storing the Category * in Object?
AliR.
Of course. The Object class has a member variable like this
Category *m_pContainer;
In the Object class, I have a public static create method where I create an
instance of Object and add it to the category's array, then initialise a
number of values on the object, such as name, description and pointer to
container. The code looks like this (second last line is the one that stores
the Category*:
Object* Object::create(Category *cat, CString sNewName)
{
Model *model = Model::instance();
// Loop round and compare the new name with all existing names
int numArtifacts = cat->GetNumberObjects();
Object *pa;
for (int i=0; i<numArtifacts; i++)
{
pa = cat->GetObject(i);
if (_stricmp(sNewName, pa->m_sName) == 0)
{
// We have found an existing object with the same
// name (not case sensitive), so fail this call
// by returning NULL
return NULL;
}
}
// Create an instance, then add to the list immediately
// This new instance goes out of scope as this method closes
Object newPA;
pa = cat->AddObject(newPA);
// Work with the returned pointer to the
// object in the array and return this pointer.
pa->m_sName = sNewName;
pa->m_dbStatus = db_new;
// pa->m_pContainer = cat;
return pa;
}
This code seems to work as the *cat always points to a valid object and the
m_pContainer on an object is never changed after this initial assignment.
"GT" <ContactGT_remove_@xxxxxxxxxxx> wrote in message
news:00e065d2$0$372$c3e8da3@xxxxxxxxxxxxxxxxxxxx
I am running into serious problems with CArray and beginning to wonder if
there is a better storage mechanism. I didn't know to define an operator=
originally (previous post), but have that all sorted now and application
is running, but with memory problems...
3 classes involved - ModelData, Category, Object
My ModelData class has a CArray of Category instances and each Category
instance has a CArray list of Object instances. Amongst the int and
CString member variables, one of the member variables on the Object class
is a pointer back up to the containing Category object.
My test application creates a series of Category instances and after
creating each Category, 5 Object instances are created and stored inside
that category (in the CArray). At creation time, the object instances
store away a pointer to the Category instance and that pointer is never
changed again in my code. After creating the 3rd Category instanceall is
well and the Object's pointers in the first 3 categories correctly point
to the Category object, but as soon as I create a 4th Category instance
and populate it, the Object's pointers in the first 3 categories are
corrupted and turn into bad_ptrs.
I don't change these pointers after they are originally setup. I
therefore presume that the Category being pointing is getting moved in
memory when the CArray object is re-sized or changed in some way. Does
this sound possible? Is there a way of storing a safe pointer in my
Object instance so that they don't keep getting lost!
Is there a better way to store this information in my application?
.
- Follow-Ups:
- Re: Is CArray best for this...
- From: GT
- Re: Is CArray best for this...
- From: AliR \(VC++ MVP\)
- Re: Is CArray best for this...
- References:
- Is CArray best for this...
- From: GT
- Re: Is CArray best for this...
- From: AliR \(VC++ MVP\)
- Is CArray best for this...
- Prev by Date: Re: Bargraphs in VC++ 6.0
- Next by Date: Re: Is CArray best for this...
- Previous by thread: Re: Is CArray best for this...
- Next by thread: Re: Is CArray best for this...
- Index(es):
Relevant Pages
|
Loading