Re: Error on deleting pointer
Re: Error on deleting pointer
- Subject: Re: Error on deleting pointer
- From: Andreas Grosam <email@hidden>
- Date: Tue, 13 Sep 2011 16:28:28 +0200
On Sep 13, 2011, at 9:24 AM, Jean-Denis MUYS wrote:
>
> On 12 sept. 2011, at 22:51, Sean McBride wrote:
>
>> On Mon, 12 Sep 2011 13:02:54 -0500, Ray, Jeffrey R. {Jeff}(DFRC-ME) said:
>>
>>> This won't help you with finding where the problem is, but you might
>>> consider adopting the coding style of setting invalid pointers to zero:
>>>
>>> if(pointer){
>>> delete pointer;
>>> pointer = 0;
>>> }
>>
>> The 'if' is not needed as the standard guarantees that 'delete NULL' does nothing. Setting the pointer to 0 afterwards can indeed be a good idea.
>
> An argument could be made that setting the pointer to NULL would tend to hide the bug not correct it. If on the contrary you want the bug to be exposed as early and systematically as possible, it would be better to use another constant. To that effect, I typically use:
>
> pointer = (void*)0xDeadBeef;
>
> Jean-Denis
I don't see what this should help, if
if (pointer) {
delete pointer;
pointer = 0; // or pointer = 0xDeadBeef;
}
is code within the d-tor. It would not prevent to accidentally create duplicates of member "pointer" in other instances of this class.
Better something like:
class Foo : boost::noncopyable {
public:
Foo() : p_(0) {}
~Foo() {
delete p_;
}
private:
X* p_;
};
Or define a class with assignment and copy c-tors (either declared private to prevent copying and assignment or defined and public which do proper things), as Glenn and Scott suggested.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden