473,401 Members | 2,125 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

delete this

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
Oct 19 '07 #1
4 1119
Internally it is simple.

delete this
=
call ThisClass::~ThisClass () ; // Destructor
free (this) ; // deallocation

It's like you're calling from anywhere else.
Of course , "delete this" must be the last statement using object
members(data or function) in the procedure you are in, because "this"
pointer is no more valid after the deletion.

delete this is usually usefoul with reference counted objects (as in
COM), but I won't suggest it's use anywhere else. You could probably
see a "delete this" in the Release function of COM objects.

Good bye
QbProg

Oct 19 '07 #2
QbProg wrote:
Internally it is simple.

delete this
=
call ThisClass::~ThisClass () ; // Destructor
free (this) ; // deallocation

It's like you're calling from anywhere else.
Of course , "delete this" must be the last statement using object
members(data or function) in the procedure you are in, because "this"
pointer is no more valid after the deletion.

delete this is usually usefoul with reference counted objects (as in
COM), but I won't suggest it's use anywhere else. You could probably
see a "delete this" in the Release function of COM objects.
One other thing to mention about 'delete this' - it's treading very close to
undefined behavior, but it's a commonly used idiom. The key to having it
work is that 'delete this' must be the very last statement executed in the
function. Any further use of the object afterwards has undefined behavior.

-cd
Oct 19 '07 #3
Thanks QbProg,
Your reply is helpful.
regards,
George

"QbProg" wrote:
Internally it is simple.

delete this
=
call ThisClass::~ThisClass () ; // Destructor
free (this) ; // deallocation

It's like you're calling from anywhere else.
Of course , "delete this" must be the last statement using object
members(data or function) in the procedure you are in, because "this"
pointer is no more valid after the deletion.

delete this is usually usefoul with reference counted objects (as in
COM), but I won't suggest it's use anywhere else. You could probably
see a "delete this" in the Release function of COM objects.

Good bye
QbProg

Oct 19 '07 #4
Thanks cd,
Your experience is very helpful.
regards,
George

"Carl Daniel [VC++ MVP]" wrote:
QbProg wrote:
Internally it is simple.

delete this
=
call ThisClass::~ThisClass () ; // Destructor
free (this) ; // deallocation

It's like you're calling from anywhere else.
Of course , "delete this" must be the last statement using object
members(data or function) in the procedure you are in, because "this"
pointer is no more valid after the deletion.

delete this is usually usefoul with reference counted objects (as in
COM), but I won't suggest it's use anywhere else. You could probably
see a "delete this" in the Release function of COM objects.

One other thing to mention about 'delete this' - it's treading very close to
undefined behavior, but it's a commonly used idiom. The key to having it
work is that 'delete this' must be the very last statement executed in the
function. Any further use of the object afterwards has undefined behavior.

-cd
Oct 19 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: foo | last post by:
I'm creating a debug class called debug_mem_allocation for the purpose of finding memory leaks. I used macro's to replace the new and delete operators. My problem is with trying to replace the...
15
by: Roy Smith | last post by:
I understand that "delete xp" deletes a scalar object and "delete xp" deletes an array of objects, but what I don't understand is why you need to tell the compiler which you're doing. When you...
13
by: Amy | last post by:
Hello, We are developing C++ appplications for PDAs where memory is limited, so we want to do memory management by ourselves --- pre-allocated a big chunk and overwrite new and delete to call...
5
by: mandatory | last post by:
hi, i have a pointer to a class, which is dynamically allocated with new. myClass *mC = new myClass(); What if i at a later point in my code i have this: void *pointer;
8
by: John Baker | last post by:
Hi: Access 2000 W98! I have a table with numerous records in it, and am attempting to delete certain records that have been selected from it. These are selected based on the ID number in a...
3
by: silver360 | last post by:
Hello, I'm trying to create a basic Heap manager and i have some question about new/delete overloading. The following code give me this output : >> $./heap >> registered : 0x804d098 >>...
10
by: junw2000 | last post by:
Hi, Below is a small code about memory allocation and deallocation. #include <cstdlib> #include <iostream> using namespace std; class X { public: void* operator new(size_t sz) throw (const...
5
by: junw2000 | last post by:
I use the code below to study delete and destructor. #include <iostream> using namespace std; struct A { virtual ~A() { cout << "~A()" << endl; }; //LINE1 void operator delete(void* p) {...
9
by: rohits123 | last post by:
I have an overload delete operator as below ////////////////////////////////// void operator delete(void* mem,int head_type) { mmHead local_Head = CPRMemory::GetMemoryHead(head_type);...
29
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, I remembered delete is implemented through operator overloading, but I am not quite clear. Could anyone recommend some links about how delete is implemented so that I can...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.