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).

3176 questions
661
votes
24 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,923
  • 4
  • 24
  • 19
597
votes
11 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
  • 57,473
  • 20
  • 96
  • 131
433
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
  • 38,276
  • 70
  • 174
  • 242
308
votes
18 answers

If you shouldn't throw exceptions in a destructor, how do you handle errors in it?

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
  • 35,641
  • 17
  • 67
  • 94
225
votes
9 answers

What is the use of having destructor as private?

What is the use of having destructor as private?
yesraaj
  • 46,370
  • 69
  • 194
  • 251
203
votes
8 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
190
votes
11 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
  • 2,035
  • 3
  • 14
  • 7
173
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
  • 18,802
  • 8
  • 49
  • 60
169
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
  • 38,276
  • 70
  • 174
  • 242
141
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
  • 4,125
  • 7
  • 28
  • 33
136
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,855
  • 6
  • 29
  • 49
130
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…
Sadeq Dousti
  • 3,346
  • 6
  • 35
  • 53
115
votes
8 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
  • 36,042
  • 30
  • 97
  • 119
114
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,284
  • 7
  • 29
  • 27
101
votes
12 answers

Is calling destructor manually always a sign of bad design?

I was thinking: they say if you're calling destructor manually - you're doing something wrong. But is it always the case? Are there any counter-examples? Situations where it is neccessary to call it manually or where it is…
Violet Giraffe
  • 32,368
  • 48
  • 194
  • 335
1
2 3
99 100