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::RemoveDe adObjects()
{
// cleanup crew!!
vector<CDrawabl eObject*>::iter ator i;
for (i = g_vecGameObject s.begin(); i !=
g_vecGameObject s.end();)
{
// if object is not null and can be removed...
if ((*i) && (*i)->CanRemove())
{
// grab hold before erasure
CDrawableObject * toDie = *i;
// erase...
i = g_vecGameObject s.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 CDrawableObject s* 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++.m oderated. First time posters: Do this! ]
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::RemoveDe adObjects()
{
// cleanup crew!!
vector<CDrawabl eObject*>::iter ator i;
for (i = g_vecGameObject s.begin(); i !=
g_vecGameObject s.end();)
{
// if object is not null and can be removed...
if ((*i) && (*i)->CanRemove())
{
// grab hold before erasure
CDrawableObject * toDie = *i;
// erase...
i = g_vecGameObject s.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 CDrawableObject s* 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++.m oderated. First time posters: Do this! ]
Comment