Questions tagged [destructor]

A special method in object-oriented programming which is invoked when an object is destroyed

Wikipedia

In object-oriented programming, a destructor (sometimes shortened to dtor) is a method which is automatically invoked when the object is destroyed. It can happen when its lifetime is bound to scope and the execution leaves the scope, when it is embedded into another object whose lifetime ends, or when it was allocated dynamically and is released explicitly. Its main purpose is to free the resources (memory allocations, open files or sockets, database connections, resource locks, etc.) which were acquired by the object along its life cycle and/or deregister from other entities which may keep references to it.

In C++, the destructor (~Class) method is core to the implementation of RAII since it is guaranteed to execute during both "normal" returns and when an exception is thrown (during stack unwinding).

2932 questions
64
votes
4 answers

How to force deletion of a python object?

I am curious about the details of __del__ in python, when and why it should be used and what it shouldn't be used for. I've learned the hard way that it is not really like what one would naively expected from a destructor, in that it is not the…
wim
  • 266,989
  • 79
  • 484
  • 630
64
votes
1 answer

Destructors of builtin types (int, char etc..)

In C++ the following code gives a compiler error: void destruct1 (int * item) { item->~int(); } This code is nearly the same, I just typedef the int to another type and something magic happens: typedef int myint; void destruct2 (myint * item) { …
Nils Pipenbrinck
  • 77,289
  • 24
  • 142
  • 216
61
votes
9 answers

Do you need to remove an event handler in the destructor?

I use some UserControls which get created and destroyed within my application during runtime (by creating and closing subwindows with these controls inside). It's a WPF UserControl and inherits from System.Windows.Controls.UserControl. There is no…
Martin Hennings
  • 14,732
  • 8
  • 38
  • 65
60
votes
7 answers

Dynamically allocating an array of objects

I have a class that contains a dynamically allocated array, say class A { int* myArray; A() { myArray = 0; } A(int size) { myArray = new int[size]; } ~A() { // Note that as per MikeB's…
Domenic
  • 100,627
  • 39
  • 205
  • 257
58
votes
7 answers

When will __destruct not be called in PHP?

class MyDestructableClass { function __construct() { print "\nIn constructor\n"; $this->name = "MyDestructableClass"; } function __destruct() { print "\nDestroying " . $this->name . "\n"; } } $obj = new…
user198729
  • 55,886
  • 102
  • 239
  • 342
58
votes
7 answers

Can the default destructor be generated as a virtual destructor automatically?

Can the default destructor be generated as a virtual destructor automatically? If I define a base class but no default destructor, is there a default virtual destructor generated automatically?
user53670
57
votes
7 answers

How do you query a pthread to see if it is still running?

In my destructor I want to destroy a thread cleanly. My goal is to wait for a thread to finish executing and THEN destroy the thread. The only thing I found about querying the state of a pthread is pthread_attr_setdetachstate but this only tells you…
Trevor Boyd Smith
  • 15,512
  • 24
  • 110
  • 159
56
votes
2 answers

Does C++ call destructors for global and class static variables?

From my example program, it looks like it does call the destructors in both the cases. At what point does it call the destructors for global and class-static variables since they should be allocated in the data section of the program stack?
user236215
  • 6,598
  • 17
  • 57
  • 83
55
votes
5 answers

What is the order in which the destructors and the constructors are called in C++

What is the order in which the destructors and the constructors are called in C++? Using the examples of some Base classes and Derived Classes
hozefam
  • 700
  • 1
  • 5
  • 9
55
votes
6 answers

Why destructor is not called on exception?

I expected A::~A() to be called in this program, but it isn't: #include struct A { ~A() { std::cout << "~A()" << std::endl; } }; void f() { A a; throw "spam"; } int main() { f(); } However, if I change last line to int main()…
Constantin
  • 25,664
  • 10
  • 57
  • 79
52
votes
1 answer

Should I default virtual destructors?

I have an abstract class that is declared as follow: class my_type { public: virtual ~my_type() = default; virtual void do_something() = 0; }; Is it considered good practice to declare the destructor like this, with the default keyword? Is…
Humam Helfawi
  • 17,706
  • 12
  • 64
  • 134
50
votes
4 answers

Goto out of a block: do destructors get called?

Consider the following code: void foo() { { CSomeClass bar; // Some code here... goto label; // and here... } label: // and here... } Will the destructor of bar be called ?
Alexandre C.
  • 52,206
  • 8
  • 116
  • 189
50
votes
5 answers

Can a destructor be recursive?

Is this program well-defined, and if not, why exactly? #include #include struct X { int cnt; X (int i) : cnt(i) {} ~X() { std::cout << "destructor called, cnt=" << cnt << std::endl; if ( cnt-- >…
Cubbi
  • 43,318
  • 13
  • 94
  • 159
49
votes
5 answers

Uses of destructor = delete;

Consider the following class: struct S { ~S() = delete; }; Shortly and for the purpose of the question: I cannot create instances of S like S s{}; for I could not destroy them. As mentioned in the comments, I can still create an instance by doing S…
skypjack
  • 45,296
  • 16
  • 80
  • 161
49
votes
3 answers

Why do we need to use virtual ~A() = default; instead of virtual ~A() {} in C++11?

In Stack Overflow post Checking the object type in C++11, I have the comment: In C++11 you'll actually want to do virtual ~A() = default; Otherwise, you'll lose the implict move constructors. What is virtual ~A() = default; for? How come implicit…
prosseek
  • 155,475
  • 189
  • 518
  • 818