C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Deleting Pointers from STL Vector
Goto page 1, 2  Next
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Hanzo
Guest





PostPosted: Thu Oct 02, 2003 10:18 am    Post subject: Deleting Pointers from STL Vector Reply with quote



I'm iterating over a vector of base class pointers and deleting those
which meet a certain criteria...i'm using pretty text-book code for
the particular delete/erasure (it's straight out of Myers' Effective
STL), and reads like this:

void CGame::RemoveDeadObjects()
{
// cleanup crew!!
vector<CDrawableObject*>::iterator i;

for (i = g_vecGameObjects.begin(); i !=
g_vecGameObjects.end()Wink
{
// if object is not null and can be removed...
if ((*i) && (*i)->CanRemove())
{
// grab hold before erasure
CDrawableObject* toDie = *i;

// erase...
i = g_vecGameObjects.erase(i);

// ...and kill
delete toDie;
toDie = NULL;
}
else
++i;
}
}

Here's the problem: on the two lines that say delete toDie; toDie =
NULL; I am noticing that the original variable to which toDie is
pointing is not being set to NULL, just "toDie" itself.

In other words, if one of the CDrawableObjects* was say, a member var
of CGame called m_cdPlayer1, when the delete/set-to-NULL happens:

1. Before the delete, toDie and m_cdPlayer1 have a value of
0x00a72498.

2. After the delete, both toDie and m_cdPlayer1 have their __vfptr
point to memory 0xFEEEFEEE (signifying a delete)

3. After the set-to-null, the value of toDie becomes 0x00000000, while
m_cdPlayer1 remains at a value of 0x00a72498 (while still pointing to
0xFEEEFEEE).

Not the behavior I want. At this point, m_cdPlayer1 is officially
deleted, yet the test "if (m_cdPlayer)" fails, since it is technically
not NULL.

Any suggestions or recommendations on how to get the intended behavior
out of this snippet?

- Hanzo

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Peter van Merkerk
Guest





PostPosted: Thu Oct 02, 2003 8:04 pm    Post subject: Re: Deleting Pointers from STL Vector Reply with quote



Quote:
I'm iterating over a vector of base class pointers and deleting those
which meet a certain criteria...i'm using pretty text-book code for
the particular delete/erasure (it's straight out of Myers' Effective
STL), and reads like this:

void CGame::RemoveDeadObjects()
{
// cleanup crew!!
vector<CDrawableObject*>::iterator i;

for (i = g_vecGameObjects.begin(); i !=
g_vecGameObjects.end()Wink
{
// if object is not null and can be removed...
if ((*i) && (*i)->CanRemove())
{
// grab hold before erasure
CDrawableObject* toDie = *i;

// erase...
i = g_vecGameObjects.erase(i);

// ...and kill
delete toDie;
toDie = NULL;
}
else
++i;
}
}

Here's the problem: on the two lines that say delete toDie; toDie =
NULL;

After those lines toDie runs out of scope; why bother setting it to
NULL?

Quote:
I am noticing that the original variable to which toDie is
pointing is not being set to NULL, just "toDie" itself.

'toDie = NULL;' only sets the toDie pointer to NULL it does not affect
the object it was pointing to. Note that that object no longer exists at
the point toDie is being set to NULL because it was deleted just before
that line.

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl





[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Randy Maddox
Guest





PostPosted: Thu Oct 02, 2003 11:44 pm    Post subject: Re: Deleting Pointers from STL Vector Reply with quote



Hanzo <hanzo (AT) milclan (DOT) com> wrote

Quote:
I'm iterating over a vector of base class pointers and deleting those
which meet a certain criteria...i'm using pretty text-book code for
the particular delete/erasure (it's straight out of Myers' Effective
STL), and reads like this:

void CGame::RemoveDeadObjects()
{
// cleanup crew!!
vector<CDrawableObject*>::iterator i;

for (i = g_vecGameObjects.begin(); i !=
g_vecGameObjects.end()Wink
{
// if object is not null and can be removed...
if ((*i) && (*i)->CanRemove())
{
// grab hold before erasure
CDrawableObject* toDie = *i;

Here toDie is a COPY of the pointer referenced by the iterator i.
When you later set toDie to NULL (and why not just 0?) you are only
setting that copy, and of course not the original pointer it was
copied from.

All you need to do is to make toDie be a reference to the pointer of
interest, rather than a copy of it. Simply change the above
declaration to:

CDrawableObject* &toDie = *i;

and all will work as you expect.

Randy.

Quote:

// erase...
i = g_vecGameObjects.erase(i);

// ...and kill
delete toDie;
toDie = NULL;
}

{ Snip of unreferenced quoting. -mod/jep }

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Bob Bell
Guest





PostPosted: Thu Oct 02, 2003 11:49 pm    Post subject: Re: Deleting Pointers from STL Vector Reply with quote

Hanzo <hanzo (AT) milclan (DOT) com> wrote

Quote:
I'm iterating over a vector of base class pointers and deleting those
which meet a certain criteria...i'm using pretty text-book code for
the particular delete/erasure (it's straight out of Myers' Effective
STL), and reads like this:

void CGame::RemoveDeadObjects()
{
// cleanup crew!!
vector<CDrawableObject*>::iterator i;

for (i = g_vecGameObjects.begin(); i !=
g_vecGameObjects.end()Wink
{
// if object is not null and can be removed...
if ((*i) && (*i)->CanRemove())
{
// grab hold before erasure
CDrawableObject* toDie = *i;

// erase...
i = g_vecGameObjects.erase(i);

// ...and kill
delete toDie;
toDie = NULL;
}
else
++i;
}
}

Here's the problem: on the two lines that say delete toDie; toDie =
NULL; I am noticing that the original variable to which toDie is
pointing is not being set to NULL, just "toDie" itself.

In other words, if one of the CDrawableObjects* was say, a member var
of CGame called m_cdPlayer1, when the delete/set-to-NULL happens:

1. Before the delete, toDie and m_cdPlayer1 have a value of
0x00a72498.

2. After the delete, both toDie and m_cdPlayer1 have their __vfptr
point to memory 0xFEEEFEEE (signifying a delete)

3. After the set-to-null, the value of toDie becomes 0x00000000, while
m_cdPlayer1 remains at a value of 0x00a72498 (while still pointing to
0xFEEEFEEE).

Not the behavior I want. At this point, m_cdPlayer1 is officially
deleted, yet the test "if (m_cdPlayer)" fails, since it is technically
not NULL.

Any suggestions or recommendations on how to get the intended behavior
out of this snippet?

Put boost::shared_ptrs in your vector (no delete needed, just erase
from the vector), and use boost::weak_ptr as m_cdPlayer1. That should
do what you want.

http://www.boost.org.

Bob

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Philipp Bachmann
Guest





PostPosted: Fri Oct 03, 2003 9:33 am    Post subject: Re: Deleting Pointers from STL Vector Reply with quote

Quote:
void CGame::RemoveDeadObjects()
{
// cleanup crew!!
vector<CDrawableObject*>::iterator i;

for (i = g_vecGameObjects.begin(); i !=
g_vecGameObjects.end()Wink
{
// if object is not null and can be removed...
if ((*i) && (*i)->CanRemove())
{
// grab hold before erasure
CDrawableObject* toDie = *i;

// erase...
i = g_vecGameObjects.erase(i);

// ...and kill
delete toDie;
toDie = NULL;
}
else
++i;
}
}

Here's the problem: on the two lines that say delete toDie; toDie =
NULL; I am noticing that the original variable to which toDie is
pointing is not being set to NULL, just "toDie" itself.

Yes, because you "delete" the object both "toDie" and your vector
element points to, but modify only the (automatic) poiter "toDie". As
usual with pointers, there's a difference between the pointer and the value /
instance it points to. So it has not much to do with "std::vector<>" nor with
C++.

Quote:
In other words, if one of the CDrawableObjects* was say, a member var
of CGame called m_cdPlayer1, when the delete/set-to-NULL happens:

Do you mean, that "m_cdPlayer1" is an instance, not a pointer? If so, you
cannot achieve your goal. So "m_cdPlayer1" _must_ be a pointer.

Quote:
Any suggestions or recommendations on how to get the intended behavior
out of this snippet?

The misconception is, that "vector<CDrawableObject*>" is the wrong type for
your usage. You should store pointers to (member) pointers instead like this:
"vector< CDrawableObjects *CGame::* > v;"
Now "delete myGame.*v[42]; myGame.*v[42]=0;"
will do the trick.

In other words, you need one further indirection in addressing.

Cheers,
Philipp.





[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Antoun Kanawati
Guest





PostPosted: Fri Oct 03, 2003 9:37 am    Post subject: Re: Deleting Pointers from STL Vector Reply with quote

Hanzo wrote:
Quote:
I'm iterating over a vector of base class pointers and deleting those
which meet a certain criteria...i'm using pretty text-book code for
the particular delete/erasure (it's straight out of Myers' Effective
STL), and reads like this:

void CGame::RemoveDeadObjects()
{
// cleanup crew!!
vector<CDrawableObject*>::iterator i;

for (i = g_vecGameObjects.begin(); i !=
g_vecGameObjects.end()Wink
{
// if object is not null and can be removed...
if ((*i) && (*i)->CanRemove())
{
// grab hold before erasure
CDrawableObject* toDie = *i;

// erase...
i = g_vecGameObjects.erase(i);

// ...and kill
delete toDie;
toDie = NULL;
}
else
++i;
}
}

Here's the problem: on the two lines that say delete toDie; toDie =
NULL; I am noticing that the original variable to which toDie is
pointing is not being set to NULL, just "toDie" itself.

In other words, if one of the CDrawableObjects* was say, a member var
of CGame called m_cdPlayer1, when the delete/set-to-NULL happens:

1. Before the delete, toDie and m_cdPlayer1 have a value of
0x00a72498.

2. After the delete, both toDie and m_cdPlayer1 have their __vfptr
point to memory 0xFEEEFEEE (signifying a delete)

3. After the set-to-null, the value of toDie becomes 0x00000000, while
m_cdPlayer1 remains at a value of 0x00a72498 (while still pointing to
0xFEEEFEEE).

Not the behavior I want. At this point, m_cdPlayer1 is officially
deleted, yet the test "if (m_cdPlayer)" fails, since it is technically
not NULL.

Any suggestions or recommendations on how to get the intended behavior
out of this snippet?

Try working with vector<CDrawableObject **>. This has enough
indirection to give you the "desired" effect. You'll need to
write:

delete *toDie;
*toDie = NULL;


Having said that, the real problem appears to be a misunderstanding
of pointers and indirection.

The variable toDie is a local variable, the assignment 'toDie = NULL'
merely sets that variable to NULL. A different variable, of the
same type and value (m_cdPlayer) is not affected at all. However,
due to the interpretation of the type (pointer to CDrawableObject),
and the statement 'delete toDie;' the variable m_cdPlayer holds a
value that is no longer valid.

The expectation that 'toDie = NULL' will affect any other variable,
under the conditions stated above, indicates a misunderstanding of
pointer indirection and iterators.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Marc Schellens
Guest





PostPosted: Fri Oct 03, 2003 11:18 am    Post subject: Re: Deleting Pointers from STL Vector Reply with quote

Quote:
I'm iterating over a vector of base class pointers and deleting those
which meet a certain criteria...i'm using pretty text-book code for
the particular delete/erasure (it's straight out of Myers' Effective
STL), and reads like this:

void CGame::RemoveDeadObjects()
{
// cleanup crew!!
vector<CDrawableObject*>::iterator i;

for (i = g_vecGameObjects.begin(); i !=
g_vecGameObjects.end()Wink
{
// if object is not null and can be removed...
if ((*i) && (*i)->CanRemove())
{
// grab hold before erasure
CDrawableObject* toDie = *i;

// erase...
i = g_vecGameObjects.erase(i);

// ...and kill
delete toDie;
toDie = NULL;
}
else
++i;
}
}

Here's the problem: on the two lines that say delete toDie; toDie =
NULL; I am noticing that the original variable to which toDie is
pointing is not being set to NULL, just "toDie" itself.

{over quote snipped -mod}

Any suggestions or recommendations on how to get the intended behavior
out of this snippet?

setting toDie = NULL is useless as it get overwritten in the next
iteration and it is unkown outside the block is is declared in anyway.

setting (*i) to NULL marks at least in your vector that the element is
deleted.
If the only pointer is in your vector, you are there. But as you
describe it, that is not the case.

Realize that you deleting an object does not change any pointer to that
object.
Probably you need to rewrite some of your program.
One possible solution would be to keep all pointers to your objects in
only one vector and using iterators to this vector to access them.

hdh,
marc







[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Hanzo
Guest





PostPosted: Fri Oct 03, 2003 11:25 am    Post subject: Re: Deleting Pointers from STL Vector Reply with quote

On 2 Oct 2003 16:04:00 -0400, "Peter van Merkerk"
<merkerk (AT) deadspam (DOT) com> wrote:

Quote:
'toDie = NULL;' only sets the toDie pointer to NULL it does not affect
the object it was pointing to. Note that that object no longer exists at
the point toDie is being set to NULL because it was deleted just before
that line.

ok, well, the goal of deleting toDie and then setting it to NULL were
the hopes of deleting the *original* variable, and setting *it* to
NULL (in this case, m_cdPlayer1).

So, in order to accomplish that, do I need to skip the entire toDie
assignment step and just:

delete *i;
*i = NULL;

?

- Hanzo

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
John Potter
Guest





PostPosted: Fri Oct 03, 2003 11:28 am    Post subject: Re: Deleting Pointers from STL Vector Reply with quote

On 2 Oct 2003 06:18:39 -0400, Hanzo <hanzo (AT) milclan (DOT) com> wrote:

Quote:
void CGame::RemoveDeadObjects()
{
// cleanup crew!!
vector<CDrawableObject*>::iterator i;

for (i = g_vecGameObjects.begin(); i !=
g_vecGameObjects.end()Wink
{
// if object is not null and can be removed...
if ((*i) && (*i)->CanRemove())
{
// grab hold before erasure
CDrawableObject* toDie = *i;

// erase...
i = g_vecGameObjects.erase(i);

This removes the pointer from the vector. Since we are about
to delete the pointee, ownership by the vector is assumed. The
whole point of this exercise is that

delete **i;
i = g_vecGameObjects.erase(i);

may have undefined behavior because we do not know what might be
done with the invalid pointer by erase. Paranoid code.

Quote:
// ...and kill
delete toDie;
toDie = NULL;
}
else
++i;
}
}

Here's the problem: on the two lines that say delete toDie; toDie =
NULL; I am noticing that the original variable to which toDie is
pointing is not being set to NULL, just "toDie" itself.

The original variable of type pointer which had the same value as
toDie has been erased from the vector and does not exist.

Quote:
In other words, if one of the CDrawableObjects* was say, a member var
of CGame called m_cdPlayer1, when the delete/set-to-NULL happens:

If that is true, there is an ownership problem and the whole program
is a total mess. The CDrawableObjects* is a pointer to an object

allowcated on the heap and owned by that pointer. If there are any
other pointers to that heap object, it is a bug in the program.

John

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Manuel
Guest





PostPosted: Fri Oct 03, 2003 11:30 am    Post subject: Re: Deleting Pointers from STL Vector Reply with quote

"Hanzo" <hanzo (AT) milclan (DOT) com> a écrit:

Quote:
I'm iterating over a vector of base class pointers and deleting those
which meet a certain criteria...i'm using pretty text-book code for
the particular delete/erasure (it's straight out of Myers' Effective
STL), and reads like this:

void CGame::RemoveDeadObjects()
{
// cleanup crew!!
vector<CDrawableObject*>::iterator i;

for (i = g_vecGameObjects.begin(); i !=
g_vecGameObjects.end()Wink
{
// if object is not null and can be removed...
if ((*i) && (*i)->CanRemove())
{
// grab hold before erasure
CDrawableObject* toDie = *i;

possible fix...
CDrawableObject*& toDie = *i;

Quote:

// erase...
i = g_vecGameObjects.erase(i);

// ...and kill
delete toDie;
toDie = NULL;
}
else
++i;
}
}
[snip]


cheers,

- Manuel
to reply, swap the name with the domain.





[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Jorge Schramm
Guest





PostPosted: Fri Oct 03, 2003 11:32 am    Post subject: Re: Deleting Pointers from STL Vector Reply with quote

Hi Hanzo,

if you want to manipulate the _original_ pointer m_cdPlayer, you must use a
vector of pointers to pointers:

void CGame::RemoveDeadObjects()
{
// cleanup crew!!
vector<CDrawableObject**>::iterator i;

for (i = g_vecGameObjects.begin(); i != g_vecGameObjects.end()Wink
{
// if object is not null and can be removed...
if ((**i) && (**i)->CanRemove())
{
delete **i;
**i = NULL;

i = g_vecGameObjects.erase(i);
}
else
++i;
}
}


Hope this helps
Jorge

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Aaron Bentley
Guest





PostPosted: Fri Oct 03, 2003 11:42 am    Post subject: Re: Deleting Pointers from STL Vector Reply with quote

Hanzo wrote:

Quote:
I'm iterating over a vector of base class pointers and deleting those
which meet a certain criteria...i'm using pretty text-book code for
the particular delete/erasure (it's straight out of Myers' Effective
STL), and reads like this:

void CGame::RemoveDeadObjects()
{
// cleanup crew!!
vector<CDrawableObject*>::iterator i;

for (i = g_vecGameObjects.begin(); i !=
g_vecGameObjects.end()Wink
{
// if object is not null and can be removed...
if ((*i) && (*i)->CanRemove())
{
// grab hold before erasure
CDrawableObject* toDie = *i;

// erase...
i = g_vecGameObjects.erase(i);

// ...and kill
delete toDie;
toDie = NULL;
}
else
++i;
}
}

Here's the problem: on the two lines that say delete toDie; toDie =
NULL; I am noticing that the original variable to which toDie is
pointing is not being set to NULL, just "toDie" itself.

Right. ToDie is just a plain pointer. You initialized it with the
value of (*i), then changed its value.

Personally, I'd rewrite it so you don't reset i immediately:
Quote:
if ((*i) && (*i)->CanRemove())
{
// erase...
Vector<CDrawableObject*>::iterator i2 =
g_vecGameObjects.erase(i);

// ...and kill
delete *i;
*i = NULL;
i=i2;
}

But the way to do what you want is a reference-to-pointer:

if ((*i) && (*i)->CanRemove())
{
// grab hold before erasure
CDrawableObject*& toDie = *i; //note ampersand

// erase...
i = g_vecGameObjects.erase(i);

// ...and kill
delete toDie;
toDie = NULL;
}
--
Aaron Bentley
www.aaronbentley.com

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
John Potter
Guest





PostPosted: Fri Oct 03, 2003 11:45 am    Post subject: Re: Deleting Pointers from STL Vector Reply with quote

On 2 Oct 2003 19:44:52 -0400, [email]rmaddox (AT) isicns (DOT) com[/email] (Randy Maddox) wrote:

Quote:
Hanzo <hanzo (AT) milclan (DOT) com> wrote


Quote:
if ((*i) && (*i)->CanRemove())
{
// grab hold before erasure
CDrawableObject* toDie = *i;

Here toDie is a COPY of the pointer referenced by the iterator i.
When you later set toDie to NULL (and why not just 0?) you are only
setting that copy, and of course not the original pointer it was
copied from.

All you need to do is to make toDie be a reference to the pointer of
interest, rather than a copy of it. Simply change the above
declaration to:

CDrawableObject* &toDie = *i;

and all will work as you expect.

I doubt it.

Quote:
// erase...
i = g_vecGameObjects.erase(i);

Make dangling reference.

Quote:
// ...and kill
delete toDie;

Kaboom!

John

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
kanze@gabi-soft.fr
Guest





PostPosted: Sat Oct 04, 2003 1:51 am    Post subject: Re: Deleting Pointers from STL Vector Reply with quote

Hanzo <hanzo (AT) milclan (DOT) com> wrote

Quote:
I'm iterating over a vector of base class pointers and deleting those
which meet a certain criteria...i'm using pretty text-book code for
the particular delete/erasure (it's straight out of Myers' Effective
STL), and reads like this:

void CGame::RemoveDeadObjects()
{
// cleanup crew!!
vector<CDrawableObject*>::iterator i;

for (i = g_vecGameObjects.begin(); i != g_vecGameObjects.end()Wink
{
// if object is not null and can be removed...
if ((*i) && (*i)->CanRemove())
{
// grab hold before erasure
CDrawableObject* toDie = *i;

// erase...
i = g_vecGameObjects.erase(i);

// ...and kill
delete toDie;
toDie = NULL;
}
else
++i;
}
}

Here's the problem: on the two lines that say delete toDie; toDie =
NULL; I am noticing that the original variable to which toDie is
pointing is not being set to NULL, just "toDie" itself.

Normal, no. You told the compiler to set toDie to NULL. You didn't tell
it to do anything else. In this case, setting toDie to NULL is for all
intents and purposes a no-op, and a any halfway good optimizing compiler
will eliminate the line completely.

I'm not sure what you mean by "the original variable". The variable
toDie contains a copy of the element which was in the vector. This
element has been erased. It doesn't exist any more, so it can't be set
to NULL.

There may, of course, be other variables designating the element. The
compiler has no way of knowing this, and it is your responsibility to
manage them. There are several alternatives, according to your needs:

- If just setting them to null is sufficient, there are different
types of smart pointers. If you use boost::shared_ptr in the array,
any boost::weak_ptr pointing to the object will appear to be NULL;
in many cases, this is a sufficient solution. If for some reason,
using boost::shared_ptr is not an alternative (although frankly, I
cannot think of one), I have a ManagedPtr at my site which will
automatically become null when the pointed to object is destructed;
it is considerably less flexible than the Boost solution, however,
since it places significant constraints on the pointed to object.

- If you are storing the pointers in other collections, say because
another object might want to refer to several GameObjects, then you
also need some way of removing the pointers from the collection
entirely, to avoid memory leaks. The simplest way to do this is
just to use weak_ptr, as above, and scan the collection periodically
(say each time you modify it) to suppress any weak_ptr whose objects
no longer exist. If the collections are really large, however, this
may have unacceptable runtime overhead. In such cases, you need
some means of informing the collection; this implies some sort of
notification mechanism, in which the collection is a listener. My
ManagedPtr uses a notification to inform the pointers to be set to
null, and could easily be adopted to handle additional
notifications. While this is definitely overkill when the first
solution works, if you do need notification, the added complexity
that ManagedPtr introduces will be necessary anyway.

- More generally, it may be that the object holding a pointer to the
deleted object needs to do more than just delete the relationship.
In this case, you need to use the Observer pattern. The destructor
of the object informs all classes which have expressed an interest
in it that it is going to die, and they do whatever they have to do.

Quote:
In other words, if one of the CDrawableObjects* was say, a member var
of CGame called m_cdPlayer1, when the delete/set-to-NULL happens:

1. Before the delete, toDie and m_cdPlayer1 have a value of
0x00a72498.

2. After the delete, both toDie and m_cdPlayer1 have their __vfptr
point to memory 0xFEEEFEEE (signifying a delete)

I think you are speaking about something in the pointed to object
itself. If both pointers point to the same address, it is normal that
reading through either pointer will result in the same thing.

Quote:
3. After the set-to-null, the value of toDie becomes 0x00000000, while
m_cdPlayer1 remains at a value of 0x00a72498 (while still pointing to
0xFEEEFEEE).

What else should (or even could) happen. The compiler has no way of
knowing which objects might or might not have pointers to the same place
as toDie. Changing toDie changes toDie, and nothing else in the
program.

Quote:
Not the behavior I want. At this point, m_cdPlayer1 is officially
deleted, yet the test "if (m_cdPlayer)" fails, since it is technically
not NULL.

Any suggestions or recommendations on how to get the intended behavior
out of this snippet?

See above. The easiest solution to begin with is to use
boost::shared_ptr in the vector, and boost::weak_ptr every where else.
But sooner or later, you'll run into a case where you need full
notification; every professional I know of has some sort of change
notification mechanism in his toolbox.

--
James Kanze GABI Software mailto:kanze (AT) gabi-soft (DOT) fr
Conseils en informatique orientée objet/ http://www.gabi-soft.fr
Beratung in objektorientierter Datenverarbeitung
11 rue de Rambouillet, 78460 Chevreuse, France, +33 (0)1 30 23 45 16

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
WW
Guest





PostPosted: Thu Oct 09, 2003 8:09 pm    Post subject: Re: Deleting Pointers from STL Vector Reply with quote

Hanzo wrote:
Quote:
I'm iterating over a vector of base class pointers and deleting those
which meet a certain criteria...i'm using pretty text-book code for
the particular delete/erasure (it's straight out of Myers' Effective
STL), and reads like this:
[SNIP]
Here's the problem: on the two lines that say delete toDie; toDie =
NULL; I am noticing that the original variable to which toDie is
pointing is not being set to NULL, just "toDie" itself.

In other words, if one of the CDrawableObjects* was say, a member var
of CGame called m_cdPlayer1, when the delete/set-to-NULL happens:

1. Before the delete, toDie and m_cdPlayer1 have a value of
0x00a72498.

2. After the delete, both toDie and m_cdPlayer1 have their __vfptr
point to memory 0xFEEEFEEE (signifying a delete)

3. After the set-to-null, the value of toDie becomes 0x00000000, while
m_cdPlayer1 remains at a value of 0x00a72498 (while still pointing to
0xFEEEFEEE).

Not the behavior I want. At this point, m_cdPlayer1 is officially
deleted, yet the test "if (m_cdPlayer)" fails, since it is technically
not NULL.

Any suggestions or recommendations on how to get the intended behavior
out of this snippet?

I see no m_Player in your code snippet. So I have no idea. However I have
few information for you:

1.) std::vector stores a copy of whatever thing it stores. The pointer
inside the vector has absolutely no connection to the m_cdPlayer1 member
variable it has been copied from.

2.) toDie is a copy of what was inside the vector. It has no connection to
the pointer pointed by the iterator. It makes absolutely no sense to set it
to NULL, since in the next line (at the }) it ceases to exist.

If you want to be able to change the original pointer, you will need to use
a pointer to pointer and store that in the vector.

--
WW aka Attila



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.