Re: Call of a destructor for NULL pointer

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance




I have understood the problem. The previous night I played with Borland C++
5.0. Today I looked through generated assembler code and saw the following

delete p; // std::auto_ptr<Widget> p;

push 0x03
mov edx,[ebp+0x08]
push dword ptr[edx]
call Widget::~Widget()

~Widget()
push ebp
mov ebp,esp
cmp dword ptr[ebp+0x08],0x00
jz +0x22
....
pop ebp
ret

i.e. the Borland C++ 5.0 compiler inserts checking of the 'this' inside a
destructor. So in debug mode I saw that Widget destructor was called.

After that I thought about to change the auto_ptr destructor the following
way :)

~auto_ptr()
{
if ( the_p ) delete the_p;
}

Vladimir Grigoriev


"Scot T Brennecke" <ScotB@xxxxxxxxxxxxxxxxxx> wrote in message
news:uOE5fc1OKHA.4580@xxxxxxxxxxxxxxxxxxxxxxx
Sure, you'll get a destructor called for temporary auto_ptr abjects
because you passed the wp parameter by value, so a copy is made. But this
doesn't have anything to do with calling a destructor for a NULL pointer.
delete p does not call a destructor if p is NULL.



.



Relevant Pages