tags:

views:

287

answers:

3

What are the instances where you need to explicitly call a destructor?

+9  A: 

When you use placement-new is a common reason (the only reason?):

struct foo {};

void* memoryLocation = ::operator new(sizeof(foo));
foo* f = new (memoryLocation) foo(); // note: not safe, doesn't handle exceptions

// ...

f->~foo();
::operator delete(memoryLocation);

This is mostly present in allocators (used by containers), in the construct and destroy functions, respectively.

Otherwise, don't. Stack-allocations will be done automatically, as it will when you delete pointers. (Use smart pointers!)

Well, I suppose that makes one more reason: When you want undefined behavior. Then feel free to call it as many times as you want... :)

GMan
@GMan: Is the ::operator really needed in calls to new and delete? With or without it, it calls the global version am I right?
jasonline
`new` and `delete` are not the same as `operator new` and `operator delete`. The keyword `new` will call `operator new` with a requested size and get raw memory back. Construct your object in that memory. The keyword `delete` will destruct the object, then call `operator delete` with the pointer to free the raw memory. I only want raw memory, and am not constructing objects, therefore I don't need `new`, only `operator new`. The `::` mean I get the global functions, which is just a habit I have.
GMan
as such it doesn't make any difference right ?? whether to use `::operator` or not
Yogesh Arora
Oh, he mixed terminology. I assumed he thought `::operator new` was a long version of `new`. If you want to call `::operator new`, if you aren't in a class that overloads `operator new`, then you can use `operator new` instead, yes.
GMan
Down-vote reason please? I'd enjoy being informed of mistakes.
GMan
@GMan: I'm kind of mixed up. Is Yogesh correct?
jasonline
It normally doesn't make any difference whether you use `::` or not. It does make a difference whether you use `operator` or not: `void* memLoc = new(sizeof(foo));` doesn't compile.
Steve Jessop
@Steve: Thanks for the clarification.
jasonline
ok now i understand what GMan said . I knew about diff between keyword `new` and `operator new` , but it never came to my mind that we can also directly call `operator new` . Thanks @GMan
Yogesh Arora
@Yogesh: No problem, it can be useful in some situations (memory management stuff, mostly.)
GMan
i had an interview yest related to similar question . I was asked how would i allocate raw memory in C++ and i said malloc. I had now idea this could be done this way
Yogesh Arora
@Yogesh: Well there you go, another way to allocate. :) Probably the preferred method too, since if your application has replaced `operator new` you'll be using that.
GMan
+4  A: 

No. You never need to explicitly call a destructor (except with placement new).

(shameless C++ FAQ Lite plug ;>)

On an extended note, calling destructors is a guarantee of the compiler -- by using new in a targeted allocation, you break that guarantee -- what is definitively a dangerous thing. If you need special allocation, it's usually better to write custom allocators and/or override new/delete.

Also take note, that calling a destructor explicitly can have extreme complications, and shouldn't be done in any other case than the case mentioned above.

Kornel Kisielewicz
Thanks for reminding me of the reference.
jasonline
A: 

Destructor are called automatically for objects of auto storage type when the object leaves scope and destructor for objects on the heap are called when the delete operator is used on them.