Is CArray best for this...



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...
    ... The Object class has a member variable like this ... number of values on the object, such as name, description and pointer to ... My ModelData class has a CArray of Category instances and each Category ... CString member variables, one of the member variables on the Object class ...
    (microsoft.public.vc.mfc)
  • 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)
  • Re: Is CArray best for this...
    ... there CArray class is free to move the ... 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 ... That array is got like this: ...
    (microsoft.public.vc.mfc)
  • 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)
  • Re: Is CArray best for this...
    ... Can you post the code where you are storing the Category * in Object? ... My ModelData class has a CArray of Category instances and each Category ... is a pointer back up to the containing Category object. ... Is there a better way to store this information in my application? ...
    (microsoft.public.vc.mfc)

Loading