Re: Is CArray best for this...



"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?





.



Relevant Pages

  • Re: Is CArray best for this...
    ... But navigating a CList is a bit more complex than CArray, since things are not index based, and you have to traverse the CList with Position variable. ... Which means that you will have to change your CArray from having a value type to a pointer type, and manage creating and destroying the elements yourself. ... the category that is passed into the method in my last post is retrieved from the CArray of Categories that is held in my ModelData class. ... 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. ...
    (microsoft.public.vc.mfc)
  • Is CArray best for this...
    ... My ModelData class has a CArray of Category instances and each Category ... member variables, one of the member variables on the Object class is a ... pointer back up to the containing Category object. ... At creation time, the object instances store ...
    (microsoft.public.vc.mfc)
  • Re: Fortran 2003: Procedure pointers
    ... > I would think you could have a pointer that could later be ... > Java has the object reference type Object, where the Object class ... data object using CLASS keyword! ...
    (comp.lang.fortran)
  • Re: Fortran 2003: Procedure pointers
    ... > I would think you could have a pointer that could later be ... > Java has the object reference type Object, where the Object class ... data object using CLASS keyword! ...
    (comp.lang.fortran)
  • Re: Is CArray best for this...
    ... CArray, since things are not index based, and you have to traverse the CList ... so a pointer to an element is not going to be valid all the ... retrieved from the CArray of Categories that is held in my ModelData ... In the Object class, I have a public static create method where I ...
    (microsoft.public.vc.mfc)

Loading