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
623
votes
22 answers

Is there a destructor for Java?

Is there a destructor for Java? I don't seem to be able to find any documentation on this. If there isn't, how can I achieve the same effect? To make my question more specific, I am writing an application that deals with data and the specification…
wai
  • 8,305
  • 4
  • 21
  • 19
502
votes
10 answers

How do I correctly clean up a Python object?

class Package: def __init__(self): self.files = [] # ... def __del__(self): for file in self.files: os.unlink(file) __del__(self) above fails with an AttributeError exception. I understand Python doesn't…
wilhelmtell
  • 53,297
  • 19
  • 89
  • 128
381
votes
7 answers

Do I need to explicitly call the base virtual destructor?

When overriding a class in C++ (with a virtual destructor) I am implementing the destructor again as virtual on the inheriting class, but do I need to call the base destructor? If so I imagine it's something like…
Nick Bolton
  • 34,516
  • 66
  • 162
  • 230
273
votes
16 answers

throwing exceptions out of a destructor

Most people say never throw an exception out of a destructor - doing so results in undefined behavior. Stroustrup makes the point that "the vector destructor explicitly invokes the destructor for every element. This implies that if an element…
Greg Rogers
  • 33,366
  • 15
  • 63
  • 93
202
votes
9 answers

What is the use of having destructor as private?

What is the use of having destructor as private?
yesraaj
  • 42,284
  • 65
  • 185
  • 246
191
votes
7 answers

When should I create a destructor?

For example: public class Person { public Person() { } ~Person() { } } When should I manually create a destructor? When have you needed to create a destructor?
delete
168
votes
11 answers

Does delete on a pointer to a subclass call the base class destructor?

I have an class A which uses a heap memory allocation for one of its fields. Class A is instantiated and stored as a pointer field in another class (class B. When I'm done with an object of class B, I call delete, which I assume calls the…
Nick Bolton
  • 34,516
  • 66
  • 162
  • 230
168
votes
2 answers

Pure virtual destructor in C++

Is it wrong to write: class A { public: virtual ~A() = 0; }; for an abstract base class? At least that compiles in MSVC... Will it crash at run time?
Ivan Krechetov
  • 17,548
  • 8
  • 45
  • 58
158
votes
12 answers

Why do we need a pure virtual destructor in C++?

I understand the need for a virtual destructor. But why do we need a pure virtual destructor? In one of the C++ articles, the author has mentioned that we use pure virtual destructor when we want to make a class abstract. But we can make a class…
Mark
  • 1,705
  • 3
  • 12
  • 7
125
votes
2 answers

Creating an object: with or without `new`

Possible Duplicate: What is difference between instantiating an object using new vs. without This is probably a basic question, and might have already been asked (say, here); yet I still don't understand it. So, let me ask it. Consider the…
M.S. Dousti
  • 2,951
  • 5
  • 31
  • 45
124
votes
10 answers

When is a C++ destructor called?

Basic Question: when does a program call a class' destructor method in C++? I have been told that it is called whenever an object goes out of scope or is subjected to a delete More specific questions: 1) If the object is created via a pointer and…
Pat Murray
  • 3,545
  • 6
  • 23
  • 32
123
votes
6 answers

How to destroy an object?

As far as I know (which is very little) , there are two ways, given: $var = new object() Then: // Method 1: Set to null $var = null; // Method 2: Unset unset($var); Other better method? Am I splitting hairs here?
PandemoniumSyndicate
  • 2,645
  • 4
  • 26
  • 49
106
votes
7 answers

What is the difference between using IDisposable vs a destructor in C#?

When would I implement IDispose on a class as opposed to a destructor? I read this article, but I'm still missing the point. My assumption is that if I implement IDispose on an object, I can explicitly 'destruct' it as opposed to waiting for the…
Jordan Parmer
  • 32,842
  • 27
  • 93
  • 118
106
votes
3 answers

In C# what is the difference between a destructor and a Finalize method in a class?

What is the difference, if there is one, between a destructor and a Finalize method in a class? I recently discovered that Visual Studio 2008 considers a destructor synonymous with a Finalize method, meaning that Visual Studio won't let you…
Jeff Leonard
  • 3,144
  • 7
  • 27
  • 27
92
votes
3 answers

shared_ptr magic :)

Mr. Lidström and I had an argument :) Mr. Lidström's claim is that a construct shared_ptr p(new Derived); doesn't require Base to have a virtual destructor: Armen Tsirunyan: "Really? Will the shared_ptr clean up correctly? Could you please in…
Armen Tsirunyan
  • 120,726
  • 52
  • 304
  • 418
1
2 3
99 100