3

Possible Duplicate:
C++: Delete this?

There is a class Foobar created on heap. I want to exit application when it dies. It must die, when I call die() function. There are some private properties created on heap - I also need to delete them. I wrote that code:

Foobar::Foobar()
{
    m_var = new int(1);
}

Foobar::~Foobar()
{
    delete m_var;
    exit(0);
}

void Foobar::die()
{
    delete this;
}

The question is in delete this line. If I call it, will Foobar::~Foobar() be called, or not?

P.S. If there is better solution, suggest it, please.

Community
  • 1
  • 1
  • 2
    any reason for a `die` function instead of calling `delete` directly? – Karthik T Dec 11 '12 at 09:38
  • 1
    The operating system reclaims heap memory when the process exits, though more generally there are types of resources that you do have to explicitly release which would be better rationales for this design. – Tony Delroy Dec 11 '12 at 09:40
  • @KarthikT yes. I use Qt and it should be called 'cos there are additional code in die() which also must be executed. – Ian P Badtrousers Dec 11 '12 at 09:41
  • you can put the code that needs to happen in `die` in `~Foobar`. Maybe safer, especially if you need access to the Foobar object for any of that code – Karthik T Dec 11 '12 at 09:44
  • @honk I created it 'cos it was hard for me to understand answers there. Now it's perfecly clear. – Ian P Badtrousers Dec 11 '12 at 09:45
  • @KarthikT in Qt there are some things which need it to be called in die() (die is a slot). – Ian P Badtrousers Dec 11 '12 at 09:47
  • Also if your additional code is after `delete this` they will not be called, since `exit` will be called before that. – Karthik T Dec 11 '12 at 09:48
  • Ah ok, if it is necessary for qt code then we cant avoid it, just be careful that `delete this` is the absolute last piece of code called. – Karthik T Dec 11 '12 at 09:49

4 Answers4

3

Yes. delete will call the destructor.

You might also be interested in reading this SO thread.


Community
  • 1
  • 1
Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345
2

Assuming the object is allocated dynamically with new: Yes, delete this will cause the destructor to be called. However, you should be very careful with deleting this. In particular, you need to ensure that no subsequent operations try to access any members of the class.

Also, if this is ever done on memory which is not dynamically allocated (i.e. with new), this leads to undefined behaviour. In fact, this also leads to undefined behaviour if the object was allocated via new[].

See this link for more information: http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.15

Here's another SO question about it: Is delete this allowed?

Community
  • 1
  • 1
Agentlien
  • 4,996
  • 1
  • 16
  • 27
  • >> However, you should be very careful with deleting `this`. Why? After that I call exit(0) which will exit application and I believe that any code won't be executed (or I am wrong?) Anyway, die() calls additional code which delete all created objects which can it's members. – Ian P Badtrousers Dec 11 '12 at 09:44
  • Sure, in this case it looks safe, but as a general rule it is always good to think twice and really make sure that you are safe whenever this pattern is employed. In this particular case, if you by `delete this` implicitly call `exit`, you really need to think about whether other resources are freed properly, as a call to `exit` means control will never return to where it came from and any resources not handled by RAII may leak. – Agentlien Dec 11 '12 at 09:51
  • Also, see my edit about how the object was allocated (whether it was via `new` or not) (as first pointed out by @Chubsdad) – Agentlien Dec 11 '12 at 09:55
  • I wrote about it in question already - "There is a class Foobar created on heap. I want to exit application when it dies..." Created on the **heap**. – Ian P Badtrousers Dec 11 '12 at 10:10
  • @IllyaKovalevskyy So I see. Still, it is a very good cautionary reminder (for other users who come by here looking for a quick answer?). So, to conclude: In your special case, there is no danger with this, as long as you're sure no other resources are leaked when calling `exit`. – Agentlien Dec 11 '12 at 10:17
1

No. This is not guaranteed. delete should only be called if we are sure that this was created using new.

So depending on how the current object was created, we may or may not have a destructor called.

So assume that the 'Foobar' object was created on stack (i.e. without new). Then delete this has an undefined behavior.

OTOH, if it was created using 'new', then delete this is safe and will invoke the destructor.

Chubsdad
  • 24,777
  • 4
  • 73
  • 129
0

It depends completely on the way the variable is defined. It is necessary that the variable is defined with the new keyword otherwise delete does not call the constructor. So declare the variable as: foobar a= new foobar();

Khawar Ali
  • 3,462
  • 4
  • 27
  • 55
  • I wrote about it in question already - "There is a class Foobar created on heap. I want to exit application when it dies..." Created on the **heap**. – Ian P Badtrousers Dec 11 '12 at 10:08