From: yodadrinkslager on
I seem to remember reading through the C++ FAQ Lite and reading an
arguement against setting a pointer to NULL after delete. Is there
such an arguement or did I imagine the whole thing?

Thanks
Allan

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

From: John H. on
yodadrinkslager wrote:
> I seem to remember reading through the C++ FAQ Lite and reading an
> arguement against setting a pointer to NULL after delete. Is there
> such an arguement or did I imagine the whole thing?

As far as the FAQ is concerned, I don't recall an argument against
setting pointer to null after delete.

If the pointer's lifetime isn't ending, I would say setting it to null
is often useful. For instance, if in your code there are some things
you want to do when there is an object being pointed to and some
different things when there is no object, setting it to null is an
easy way to tell if there is an object or not.
If the pointer's lifetime is about up, for instance a class member
variable in the class's destructor, then it is less useful to set it
to null.

Also, some upcoming versions of C++ are going to have a nullptr
keyword which you can use instead of NULL.

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

From: Andrey Tarasevich on
yodadrinkslager wrote:
> I seem to remember reading through the C++ FAQ Lite and reading an
> arguement against setting a pointer to NULL after delete. Is there
> such an arguement or did I imagine the whole thing?

Yes, there is.

The popular legend about setting a pointer to null after `delete` is
that is will allegedly make you safe you from the double-delete problem
(i.e. when the same pointer value is passed to `delete` more than once).

In reality, the _real_ double-delete problem occurs most of the time
when the same memory block is `delete`d multiple times through two or
more _different_ pointer objects holding the same address. Needless to
say, setting a pointer to null after `delete` will achieve absolutely
nothing to prevent the problem.

While it is true that setting a pointer to null after `delete` might
save you from double-delete through the same pointer, in reality in 9
cases out of 10 such a situation indicates a more generic problem with
the overall logic of the code. Treating it as a mere double-delete in
general case will only threat a symptom of the problem, not the problem
itself, i.e. it will just sweep the issue under the carpet.

--
Best regards,
Andrey Tarasevich

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

From: Hakusa on
On Nov 11, 8:22 pm, Andrey Tarasevich <andreytarasev...(a)hotmail.com>
wrote:

>> Is there
>> such an arguement or did I imagine the whole thing?

> Yes, there is.

> While it is true that setting a pointer to null after `delete` might
> save you from double-delete through the same pointer, in reality in 9
> cases out of 10 such a situation indicates a more generic problem with
> the overall logic of the code. Treating it as a mere double-delete in
> general case will only threat a symptom of the problem, not the problem
> itself, i.e. it will just sweep the issue under the carpet.

Are you presenting an argument against NULLing a pointer after delete,
meaning doing so is completely useless, or simply saying that it's not
a cure-all? You seem to claim (by "Yes, there is") that you present
such an argument, but after reading it i still feel it's a good idea
to null one's pointers, but that other precautions (such as using a
smart pointer?) should be considered.

Would you mind clarifying?


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

From: Seungbeom Kim on
Andrey Tarasevich wrote:
> yodadrinkslager wrote:
>> I seem to remember reading through the C++ FAQ Lite and reading an
>> arguement against setting a pointer to NULL after delete. Is there
>> such an arguement or did I imagine the whole thing?
>
> Yes, there is.
>
> The popular legend about setting a pointer to null after `delete` is
> that is will allegedly make you safe you from the double-delete problem
> (i.e. when the same pointer value is passed to `delete` more than once).
>
> In reality, the _real_ double-delete problem occurs most of the time
> when the same memory block is `delete`d multiple times through two or
> more _different_ pointer objects holding the same address. Needless to
> say, setting a pointer to null after `delete` will achieve absolutely
> nothing to prevent the problem.
>
> While it is true that setting a pointer to null after `delete` might
> save you from double-delete through the same pointer, in reality in 9
> cases out of 10 such a situation indicates a more generic problem with
> the overall logic of the code. Treating it as a mere double-delete in
> general case will only threat a symptom of the problem, not the problem
> itself, i.e. it will just sweep the issue under the carpet.
>

I agree. When you encounter a double-delete problem that you didn't
expect, the correct solution is not to make the double-delete innocuous
by setting the pointer to null (which won't work in many cases as
mentioned above, or which will just hide the problem at best), but
to find out why it is happening even though it is not intended, and
to fix it so that it won't happen again.

--
Seungbeom Kim

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