George2

Hello everyone,


Just interested to learn how C++ implements delete this statement internally. From logical point of view, I can not imagine how to implement a component which destroy itself.

What makes me confused is the component is relying on itself even in the process of destroy, how could it destroys itself from itself

thanks in advance,
George



Re: Visual C++ Language delete this pointer

Ramkrishna Pawar

delete this gets converted into delete [address_value_of_current_Object] , whcih is essentially the same as deleteing any other pointer.

But care should be taken while calling delete this; as it is not necessory to have this object allocated using new, it could be a local variable on which delete call is not correct.

class XYZ

{//

void SomeFunction() { delete this; } // example use

};

void AnotherFunction()

{

XYZ myX;

myX.SomeFunction(); // this would make use of delete, which might turn into a disaster.

}






Re: Visual C++ Language delete this pointer

Viorel.

Since "delete this" deletes member data, but does not destroy code, the execution of the member function, where statement appears, can continue. Obviously you cannot next access non-static data members or virtual functions, since this pointer is not more valid.





Re: Visual C++ Language delete this pointer

George2

Thanks Ramkrishna,

Ramkrishna Pawar wrote:

delete this gets converted into delete [address_value_of_current_Object] , whcih is essentially the same as deleteing any other pointer.

But care should be taken while calling delete this; as it is not necessory to have this object allocated using new, it could be a local variable on which delete call is not correct.

class XYZ

{//

void SomeFunction() { delete this; } // example use

};

void AnotherFunction()

{

XYZ myX;

myX.SomeFunction(); // this would make use of delete, which might turn into a disaster.

}

Your sample makes senses. But it is hard in C++ to detect whether a variable is on stack or on heap. So, how to avoid such kind of problem in your experiences

regards,

George





Re: Visual C++ Language delete this pointer

George2

Thanks Viorel,

Viorel. wrote:

Since "delete this" deletes member data, but does not destroy code, the execution of the member function, where statement appears, can continue. Obviously you cannot next access non-static data members or virtual functions, since this pointer is not more valid.

Your reply makes senses.

Have a good weekend,

George





Re: Visual C++ Language delete this pointer

Pintu Shukla

George2 wrote:

But it is hard in C++ to detect whether a variable is on stack or on heap. So, how to avoid such kind of problem in your experiences

regards,

George

just try to remember whenever you use new in your code to allocate memory to your object this means you are creating your object on heap. otherwise on stack . Second thing if you are using delete this in some function of a class it is valid .But if you are calling delete this inside your destructor that time your program will make a infinite loop. so calling delete this inside a member function of class is properly valid .here is a plain COM example

Code Block

ULONG className::Addref()

{

return ++m_RefCount;

}

ULONG className::Release()

{

--m_RefCount;

if(m_RefCount==0)

{

delete this;

return 0;

}

return m_RefCount;

}

but never try to call delete this in your destructor.

Thanx






Re: Visual C++ Language delete this pointer

Ramkrishna Pawar

George2 wrote:

Thanks Ramkrishna,

Ramkrishna Pawar wrote:

delete this gets converted into delete [address_value_of_current_Object] , whcih is essentially the same as deleteing any other pointer.

But care should be taken while calling delete this; as it is not necessory to have this object allocated using new, it could be a local variable on which delete call is not correct.

class XYZ

{//

void SomeFunction() { delete this; } // example use

};

void AnotherFunction()

{

XYZ myX;

myX.SomeFunction(); // this would make use of delete, which might turn into a disaster.

}

Your sample makes senses. But it is hard in C++ to detect whether a variable is on stack or on heap. So, how to avoid such kind of problem in your experiences

regards,

George

The only time someone needs to use delete this is when new throws an exception, and your object is partly constructed. One should not at all use delete this; generally, instead you can use an allocator function, which deletes the object in case of exception. At least you should make it sure that delete this is not called if the object is not constructed using new.






Re: Visual C++ Language delete this pointer

Jonathan Caves - MSFT

Ramkrishna Pawar wrote:

The only time someone needs to use delete this is when new throws an exception, and your object is partly constructed.

This is not correct - please see the ref-counted example shown above. In fact in the case you mention you should not need to call "delete this".






Re: Visual C++ Language delete this pointer

George2

Thanks Pintu,

I think you mean if we invoke delete this in destructor, since delete this itself will invoke destructor, so there will be an infinite loop, right

regards,

George

Pintu Shukla wrote:

George2 wrote:

But it is hard in C++ to detect whether a variable is on stack or on heap. So, how to avoid such kind of problem in your experiences

regards,

George

just try to remember whenever you use new in your code to allocate memory to your object this means you are creating your object on heap. otherwise on stack . Second thing if you are using delete this in some function of a class it is valid .But if you are calling delete this inside your destructor that time your program will make a infinite loop. so calling delete this inside a member function of class is properly valid .here is a plain COM example

Code Block

ULONG className::Addref()

{

return ++m_RefCount;

}

ULONG className::Release()

{

--m_RefCount;

if(m_RefCount==0)

{

delete this;

return 0;

}

return m_RefCount;

}

but never try to call delete this in your destructor.

Thanx





Re: Visual C++ Language delete this pointer

George2

Thanks Ramkrishna,

What do you mean *allocator function*

regards,

George

Ramkrishna Pawar wrote:

George2 wrote:

Thanks Ramkrishna,

Ramkrishna Pawar wrote:

delete this gets converted into delete [address_value_of_current_Object] , whcih is essentially the same as deleteing any other pointer.

But care should be taken while calling delete this; as it is not necessory to have this object allocated using new, it could be a local variable on which delete call is not correct.

class XYZ

{//

void SomeFunction() { delete this; } // example use

};

void AnotherFunction()

{

XYZ myX;

myX.SomeFunction(); // this would make use of delete, which might turn into a disaster.

}

Your sample makes senses. But it is hard in C++ to detect whether a variable is on stack or on heap. So, how to avoid such kind of problem in your experiences

regards,

George

The only time someone needs to use delete this is when new throws an exception, and your object is partly constructed. One should not at all use delete this; generally, instead you can use an allocator function, which deletes the object in case of exception. At least you should make it sure that delete this is not called if the object is not constructed using new.





Re: Visual C++ Language delete this pointer

George2

Thanks Jonathan,

Jonathan Caves - MSFT wrote:

Ramkrishna Pawar wrote:

The only time someone needs to use delete this is when new throws an exception, and your object is partly constructed.

This is not correct - please see the ref-counted example shown above. In fact in the case you mention you should not need to call "delete this".

I agree. Actually my question comes from some COM code as you mentioned.

have a good weekend,

George





Re: Visual C++ Language delete this pointer

Pintu Shukla

George2 wrote:

Thanks Pintu,

I think you mean if we invoke delete this in destructor, since delete this itself will invoke destructor, so there will be an infinite loop, right



Yes .That's why never call delete this in the destructor. Otherwise you would run into a recursive call of your destructor and this will end up in a stack overflow, because delete this will firstly call the destructor (which would call delete this again) .

Thanx







Re: Visual C++ Language delete this pointer

George2

Thanks Pintu,

Pintu Shukla wrote:

George2 wrote:

Thanks Pintu,

I think you mean if we invoke delete this in destructor, since delete this itself will invoke destructor, so there will be an infinite loop, right



Yes .That's why never call delete this in the destructor. Otherwise you would run into a recursive call of your destructor and this will end up in a stack overflow, because delete this will firstly call the destructor (which would call delete this again) .

Thanx


I am clear now.

regards,

George