Re: Is CArray best for this...
- From: "AliR \(VC++ MVP\)" <AliR@xxxxxxxxxxxxx>
- Date: Wed, 16 Jan 2008 17:40:29 GMT
Reading through the documentation, there CArray class is free to move the
data around, so a pointer to an element is not going to be valid all the
time. (I saw that in the docs for CArray::SetSize)
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.
AliR.
"GT" <ContactGT_remove_@xxxxxxxxxxx> wrote in message
news:478e399e$0$25171$c3e8da3@xxxxxxxxxxxxxxxxxxxx
"AliR (VC++ MVP)" <AliR@xxxxxxxxxxxxx> wrote in message
news:yBqjj.33378$4V6.19553@xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
How about the call to create what does that look like?
What I am trying to see is the code where you get the pointer of the
Category object that is ultimately stored in the Object object.
When I step into the method in my last post, the category object is valid
and correct and when stored in the Object, seems to still be valid and
correct. The first part of the previously posted method wouldn't run if
the pointer wasn't valid.
However, 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. The ModelData class is instantiated once in my main application as
'm_theProjectData'. At the moment I have kept everything public to keep
development time and thought down (this is just a prototype). So to get to
a category, I go to the ModelData instance, refer to its array of
categories and use GetAt. That array is got like this:
&m_theProjectData.m_arrayCategories.GetAt(num);
If CArray::GetAt() returns a reference to the actual object then there
should be no problem, but if GetAt returns a copy, then that would explain
everything - I am using a pointer to a temporary copy of something that
goes of scope immediately after I setup a pointer to it and the space it
occupies may or may not be overwritten in memory. However, I have only
just realised this as I write this down - sometimes just explaining things
helps!
AliR.
"GT" <ContactGT_remove_@xxxxxxxxxxx> wrote in message
news:478e33bd$0$25231$c3e8da3@xxxxxxxxxxxxxxxxxxxx
"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: 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\)
- Re: Is CArray best for this...
- From: GT
- Re: Is CArray best for this...
- From: AliR \(VC++ MVP\)
- Re: Is CArray best for this...
- From: GT
- Is CArray best for this...
- Prev by Date: Re: Does OnCtlColor create GDI resource leak?
- Next by Date: Re: Bargraphs in VC++ 6.0
- Previous by thread: Re: Is CArray best for this...
- Next by thread: Re: Is CArray best for this...
- Index(es):
Relevant Pages
|
Loading