Destructor (computer programming)
In object-oriented programming, a destructor is a method which is invoked mechanically just before the memory of the object is released. It can happen either when its lifetime is bound to scope and the execution leaves the scope, when it is embedded in another object whose lifetime ends, or when it was allocated dynamically and is released explicitly. Its main purpose is to free the resources which were acquired by the object during its life and/or deregister from other entities which may keep references to it. Destructors are necessary in resource acquisition is initialization.
With most kinds of automatic garbage collection algorithms, the releasing of memory may happen a long time after the object becomes unreachable, making destructors unsuitable for time-critical purposes. In these languages, the freeing of resources is done through an lexical construct, or by explicitly calling a function ; in particular, many object-oriented languages use the dispose pattern.
Syntax
- C++: destructors have the same name as the class with which they are associated, but with a tilde prefix has a destructor
~X). - C#: same syntax as C++. Historically called destructors, now called finalizers due to confusion.
- D: declared as
~this. - Java: provided by 2 interfaces,
CloseableandAutoCloseable. In Java 9+, destructors are replaced byCleaner. Java also used to haveObject.finalize, which was also deprecated. - Object Pascal: destructor methods have the keyword
destructorand can be any name, but convention isDestroy. - Objective-C: destructor method is named
dealloc. - Perl: destructor method is named
DESTROY; in the Moose object system extension, it is namedDEMOLISH. - PHP: In PHP 5+, destructor method is named
__destruct. There were no destructors in prior versions of PHP. - Python: destructor method is named
__del__. Called destructors in Python 2, now called finalizers in Python 3. - Rust: destructor method is named
dropand is provided by theDroptrait. - Swift: destructor method is named
deinit.
delete exists in JavaScript and TypeScript, but unlike in C++, delete in those language removes a property from a class.Language details
C
As C does not have objects, it has neither constructors nor destructors. However, they can be emulated using functions that allocate and destroy, to abstract away manual calls tomalloc and free.- include
- include
// The "constructor"
Resource* createResource
// The "destructor"
void destroyResource
int main
GCC extensions
The GNU Compiler Collection's C compiler comes with 2 extensions that allow implementing destructors:- The
destructorfunction attribute allows defining global prioritized destructor functions: whenmainreturns, these functions are called in priority order before the process terminates. See also: Hacking the art of exploitation. - The cleanup variable attribute allows attaching a destructor function to a variable: the function is called when the variable goes out of scope.
C++
Foo will have the destructor. Additionally, destructors have neither parameters nor return types. As stated above, a destructor for an object is called whenever the object's lifetime ends. If the object was created as an automatic variable, its lifetime ends and the destructor is called automatically when the object goes out of scope. Because C++ does not have garbage collection, if the object was created with a new and delete | statement, then its destructor is called when the delete | operator is applied to a pointer to the object. Usually that operation occurs within another destructor, typically the destructor of a smart pointer object.In inheritance hierarchies, the declaration of a virtual destructor in the base class ensures that the destructors of derived classes are invoked properly when an object is deleted through a pointer-to-base-class. Objects that may be deleted in this way need to inherit a virtual destructor.
A destructor should never throw an exception.
Non-class scalar types have what's called a which can be accessed by using
typedef or template arguments. This construct makes it possible to write code without having to know if a destructor exists for a given type.int f
In older versions of the standard, pseudo-destructors were specified to have no effect, however that was changed in a defect report to make them end the lifetime of the object they are called on.
Objects which cannot be safely copied and/or assigned should be disabled from such semantics by declaring their corresponding functions as deleted. A detailed description of this method can be found in Scott Meyers' popular book, Effective Modern C++. If they are marked
deleted, they should be public so that accidental uses do not warn that they are private, but explicitly deleted. Since C++26, it is possible to specify a reason for the deletion.Example
import std;
using std::formatter;
using std::format_parse_context;
class Foo ;
template <>
struct formatter
int main
By using smart pointers with the "Resource Acquisition is Initialization" idiom, manual resource cleanup can be abstracted. Other languages like Java and C# include a
finally block for cleanup, however C++ does not have the finally block and instead encourages using the RAII idiom.import std;
using std::formatter;
using std::format_parse_context;
using std::unique_ptr;
class Foo ;
template <>
struct formatter
int main
C#
Destructors in C# are not manually called or called by adelete operator like in C++. They are only called by the garbage collector.using System;
class MyClass
class Program
C# also has a "dispose" pattern in which the class must implement the interface
IDisposable. C# supports try-with-resources blocks similar to Java, called using-with-resources. A class must implement IDisposable to be used in a using-with-resources block.using System;
class MyClass : IDisposable
class Program
Java
Java provides 2 interfaces that implement destructors,java.lang.Closeable and java.lang.AutoCloseable. A class that implements AutoCloseable is able to be used in a "try-with-resources" block, available since Java 7.final class Destructible implements AutoCloseable
public final class Example
Prior to Java 7, a "try-finally" block was used.
final class Destructible
public final class Example
Historically, Java used
Object.finalize instead, however this has been deprecated. Java has a method called System.gc to suggest the Java Virtual Machine to perform garbage collection, which internally calls Runtime.getRuntime.gc, which requests the garbage collector to trigger a garbage collection cycle, but this is not guaranteed, as the JVM manages memory independently. System.gc may lead to finalize being called, but only if the object is eligible for garbage collection and has a finalize method.class ParentFinalizerExample
class FinalizerExample extends ParentFinalizerExample
public class Example
Java also supports classes
java.lang.ref.Cleaner and java.lang.ref.PhantomReference for safer low-level cleanup. Cleaner was introduced in Java 9 and is more efficient than PhantomReference, and works by registering an object with a cleaner thread which runs a cleanup action once the object is unreachable.import java.lang.ref.Cleaner;
import java.lang.ref.Cleaner.Cleanable;
class Resource
public class Example
PhantomReference, since Java 1.2, is an older cleanup mechanism that uses a reference queue. PhantomReference is used solely for being notified that an object's garbage collection is pending. Once the object is garbage collected, the PhantomReference is enqueued into the ReferenceQueue. Unlike WeakReference which can be used to access the object if it still exists in memory, PhantomReference can only be used for detecting when an object will be destroyed. Both PhantomReference and WeakReference do not increase reference counts. import java.lang.ref.PhantomReference;
import java.lang.ref.ReferenceQueue;
class Resource
public class PhantomReferenceExample
Unlike to
std::weak_ptr in C++ which is used to reference an object without increasing its reference count, WeakReference can still access the object after the reference count reaches 0, whereas in C++ std::weak_ptr cannot as the object is immediately destroyed once the object reaches 0 references.