Re: Deleting Pointers from STL Vector
On 2 Oct 2003 19:44:52 -0400, rmaddox@isicns. com (Randy Maddox) wrote:
[color=blue]
> Hanzo <hanzo@milclan. com> wrote in message news:<omhnnv4l0 nfkbdv7014dovil 94s2ang3g1@4ax. com>...[/color]
[color=blue][color=green]
> > if ((*i) && (*i)->CanRemove())
> > {
> > // grab hold before erasure
> > CDrawableObject * toDie = *i;[/color][/color]
[color=blue]
> 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.[/color]
[color=blue]
> 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:[/color]
[color=blue]
> CDrawableObject * &toDie = *i;
>
> and all will work as you expect.[/color]
I doubt it.
[color=blue][color=green]
> > // erase...
> > i = g_vecGameObject s.erase(i);[/color][/color]
Make dangling reference.
[color=blue][color=green]
> > // ...and kill
> > delete toDie;[/color][/color]
Kaboom!
John
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]
On 2 Oct 2003 19:44:52 -0400, rmaddox@isicns. com (Randy Maddox) wrote:
[color=blue]
> Hanzo <hanzo@milclan. com> wrote in message news:<omhnnv4l0 nfkbdv7014dovil 94s2ang3g1@4ax. com>...[/color]
[color=blue][color=green]
> > if ((*i) && (*i)->CanRemove())
> > {
> > // grab hold before erasure
> > CDrawableObject * toDie = *i;[/color][/color]
[color=blue]
> 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.[/color]
[color=blue]
> 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:[/color]
[color=blue]
> CDrawableObject * &toDie = *i;
>
> and all will work as you expect.[/color]
I doubt it.
[color=blue][color=green]
> > // erase...
> > i = g_vecGameObject s.erase(i);[/color][/color]
Make dangling reference.
[color=blue][color=green]
> > // ...and kill
> > delete toDie;[/color][/color]
Kaboom!
John
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]
Comment