Jump to content

Destructor (computer programming)

From Wikipedia, the free encyclopedia

In object-oriented programming, a destructor (sometimes abbreviated dtor[1]) is a method which is invoked mechanically just before the memory of the object is released.[2] 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 (memory allocations, open files or sockets, database connections, resource locks, etc.) 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 (RAII).

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 (such as try-finally, Python's with, or Java's "try-with-resources"), or by explicitly calling a function (equivalent to explicit deletion); in particular, many object-oriented languages use the dispose pattern.

Syntax

[edit]
  • C++: destructors have the same name as the class with which they are associated, but with a tilde prefix (for example, a class X with a constructor X() has a destructor ~X()).[2]
  • C#: same syntax as C++. Historically called destructors, now called finalizers due to confusion.[3]
  • D: declared as ~this() (whereas constructors are declared as this()).
  • Java: provided by 2 interfaces, Closeable (deprecated) and AutoCloseable. In Java 9+, destructors are replaced by Cleaner. Java also used to have Object.finalize(), which was also deprecated.
  • Object Pascal: destructor methods have the keyword destructor and can be any name, but convention is Destroy.
  • Objective-C: destructor method is named dealloc.
  • Perl: destructor method is named DESTROY; in the Moose object system extension, it is named DEMOLISH.
  • PHP: In PHP 5+, destructor method is named __destruct. There were no destructors in prior versions of PHP.[4]
  • Python: destructor method is named __del__. Called destructors in Python 2,[5] now called finalizers in Python 3.[6]
  • Rust: destructor method is named drop and is provided by the Drop trait.[7]
  • Swift: destructor method is named deinit.

Language details

[edit]

C++

[edit]

The destructor has the same name as the class, but with a tilde (~) before it.[2] For example, a class called Foo will have the destructor ~Foo(). Additionally, destructors have neither parameters nor return types.[2] As stated above, a destructor for an object is called whenever the object's lifetime ends.[2] 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 statement (dynamically on the heap), 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.[8]

Non-class scalar types have what's called a pseudo-destructor 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() {
    int a = 123;
    using T = int;
    a.~T();
    return a; // undefined behavior
}

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.[9]

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++ (Item 11: "Prefer deleted functions to private undefined ones."[10]). 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

[edit]
import std;

class Foo {
private:
    char data[];
    friend struct std::formatter<Foo>;
public:
    // Constructor
    explicit Foo(const char* s = ""): 
        data{new char[std::strlen(s) + 1]} {
        std::strcpy(data, s);
    }

    Foo(const Foo& other) = delete("Copy construction disabled");
    Foo& operator=(const Foo& other) = delete("Copy assignment disabled");

    // Destructor
    ~Foo() { 
        delete[] data; 
    }
};

template <>
struct std::formatter<Foo> {
    constexpr auto parse(std::format_parse_context& ctx) -> const char* {
        return ctx.end();
    }

    template <typename FormatContext>
    auto format(const Foo& foo, FormatContext& ctx) -> FormatContext::iterator {
        return std::format_to(ctx.out(), "{}", foo.data);
    }
};

int main(int argc, char* argv[]) {
    Foo foo("Hello from the stack!");
    std::println("{}", foo);

    Foo* foo = new Foo("Hello from the heap!");
    std::println("{}", *foo);
    delete foo;
}

By using smart pointers with the "Resource Acquisition is Initialization" (RAII) idiom, manually defining destructors or calling resource cleanup can be bypassed. 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;

class Foo {
private:
    std::unique_ptr<char[]> data;
    friend struct std::formatter<Foo>;
public:
    // Constructor
    explicit Foo(const char* s = ""):
        data{std::make_unique<char[]>(std::strlen(s) + 1)} {
        std::strcpy(data.get(), s);
    }

    Foo(const Foo& other) = delete("Copy construction disabled");
    Foo& operator=(const Foo& other) = delete("Copy assignment disabled");

    // Destructor is automatically handled by unique_ptr
    ~Foo() = default;
};

template <>
struct std::formatter<Foo> {
    constexpr auto parse(std::format_parse_context& ctx) -> const char* {
        return ctx.end();
    }

    template <typename FormatContext>
    auto format(const Foo& foo, FormatContext& ctx) -> FormatContext::iterator {
        return std::format_to(ctx.out(), "{}", foo.data.get());
    }
};

int main(int argc, char* argv[]) {
    Foo foo("Hello from the stack!");
    std::println("{}", foo);

    std::unique_ptr<Foo> foo = std::make_unique<Foo>("Hello from the heap!");
    std::println("{}", *foo);
}

C#

[edit]

Destructors in C# are not manually called or called by a delete operator like in C++. They are only called by the garbage collector.

UML class in C# containing a constructor and a destructor.
using System;

class MyClass
{
    private string resource;

    public MyClass(string resourceName)
    {
        resource = resourceName;
    }

    ~MyClass()
    {
        Console.WriteLine($"Destructor called to clean up resource: {resource}");
        // cleanup code
    }
}

class Program
{
    static void Main(string[] args)
    {
        MyClass obj = new MyClass("Sample Resource");
        
        GC.Collect();
        GC.WaitForPendingFinalizers();

        Console.WriteLine("Program finished");
    }
}

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
{
    private string resource;
    private bool disposed = false; // To detect redundant calls to Dispose()

    public MyClass(string resourceName)
    {
        resource = resourceName;
    }

    ~MyClass()
    {
        Console.WriteLine("Destructor called");
        Dispose(false);
    }

    public void Dispose()
    {
        Console.WriteLine("Disposer called");
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    protected virtual void Dispose(bool disposing)
    {
        if (!disposed)
        {
            if (disposing)
            {
                Console.WriteLine("Disposing managed resources.");
            }
            Console.WriteLine("Disposing unmanaged resources.");
            disposed = true;
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        using (Destructible d = new Destructible())
        {
            Console.WriteLine("Using Destructible.");
            // ...
        }
        // after using-with-resources, d.Dispose() will be called
    }
}

C with GCC extensions

[edit]

The GNU Compiler Collection's C compiler comes with 2 extensions that allow implementing destructors:

  • The destructor function attribute[11] allows defining global prioritized destructor functions: when main() returns, these functions are called in priority order before the process terminates. See also: Hacking the art of exploitation.[12]
  • The cleanup variable attribute allows attaching a destructor function to a variable: the function is called when the variable goes out of scope.

Java

[edit]

Java provides 2 interfaces that implement destructors, java.lang.Closeable (deprecated) and java.lang.AutoCloseable. A class that implements AutoCloseable is able to be used in a "try-with-resources" block, available since Java 7.[13]

final class Destructible implements AutoCloseable {
    @Override
    public void close() {
        // cleanup code 
    }
}

public final class Example {
    try (Destructible d = new Destructible()) {
        System.out.println("Using Destructible.");
        // ...
    }
    // after try-with-resources, d.close() will be called
    
}

Prior to Java 7, a "try-finally" block was used.

final class Destructible {
    public void close() { 
        // cleanup code
    }
}

public final class Example {
    try {
        Destructible d = new Destructible();
        System.out.println("Using Destructible.");
    } finally {
        d.close()
    }
    
}

Historically, Java used Object.finalize() instead, however this has been deprecated. Java has a method called System.gc() to suggest the Java Virtual Machine (JVM) 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 {
    @Override
    protected void finalize() throws Throwable {
        try {
            System.out.println("finalize() called, cleaning up...");
        } finally {
            super.finalize(); // Always call super.finalize() to clean parent classes
        }
    }
}

public class Example {
    public static void main(String[] args) {
        FinalizerExample obj = new FinalizerExample ();
        obj = null;
        System.gc(); // Requests garbage collection (not guaranteed)
    }
}

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 (i.e. no references to it exist).

import java.lang.ref.Cleaner;
import java.lang.ref.Cleaner.Cleanable;

class Resource {
    private static final Cleaner cleaner = Cleaner.create();

    static class State implements Runnable {
        private boolean cleaned = false;

        @Override
        public void run() {
            cleaned = true;
            System.out.println("Cleaned using Cleaner");
        }
    }

    private final State state;
    private final Cleanable cleanable;

    public Resource () {
        this.state = new State();
        this.cleanable = cleaner.register(this, state);
    }

    public void cleanup() {
        System.gc(); // Request garbage collection (not guaranteed)
    }
}

public class Example {
    public static void main(String[] args) {
        Resource resource = new Resource();
        resource = null;
        resource.cleanup();
    }
}

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 {
    private final String name;

    public Resource(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

public class PhantomReferenceExample {
    public static void main(String[] args) throws InterruptedException {
        Resource resource = new Resource("My resource");
        ReferenceQueue<Resource> queue = new ReferenceQueue<>();
        PhantomReference<Resource> phantomRef = new PhantomReference<>(resource, queue);

        resource = null;

        System.gc();

        Reference<? extends Resource> ref = queue.poll();
        if (ref != null) {
            System.out.printf("Object is ready to be collected: %s%n", ((PhantomReference<?>)ref).get());
        }
    }
}

Python

[edit]

Python supports destructors and has a del keyword, but unlike delete in C++, del only decreases the reference count of the object, and does not necessarily immediately destroy the object.

class Destructible:
    def __init__(self, name: str) -> 'Destructible':
        self.name = name
        print(f"Created Destructible: {self.name}")

    def __del__(self) -> None:
        print(f"Destructor called for: {self.name}")

if __name__ == "__main__":
    d: Destructible = Destructible("My name")
    print(f"Using Destructible: {d.name}")
    del d

Much like Java and C#, Python has a try-with-resources block, called a with block or a "context manager". It is used for things like files and network connections.

from typing import Optional

class Destructible:
    def __init__(self, name: str) -> None:
        self.name: str = name
    
    def __enter__(self) -> 'Destructible':
        print(f"Entering context (allocating resource: {self.name})")
        return self

    def __exit__(self, exc_type: Optional[type], exc_val: Optional[Exception], exc_tb: Optional[type]) -> None:
        print(f"Exiting context (cleaning up resource: {self.name})")

if __name__ == "__main__":
    with Destructible("Resource A") as d:
        print(f"Using resource {d.name} inside context")

    # Most Python standard library resources support with blocks:
    with open(file_path, 'r') as file:
        print("Reading the file content:")
        content: str = file.read()
        print(content)

Rust

[edit]

Rust does not have destructors in the sense of object-oriented programming, but a struct can implement the Drop trait and the drop method to clean itself up after it goes out of scope.

It is not possible to destroy objects explicitly through a delete operator like in C++, though it is possible to manually call drop() prematurely by using std::mem::drop().

use std::mem;

struct Destructible {
    name: String,
}

impl Destructible {
    fn new(name: String) -> Self {
        Destructible { name }
    }
}

impl Drop for Destructible {
    fn drop(&mut self) {
        println!("Dropping Destructible: {}", self.name);
    }
}

fn main() {
    {
        let resource_a: Destructible = Destructible::new(String::from("Resource A"));
        println!("Using Destructible.");
    } // <--- resource_a goes out of scope here, `drop()` is called automatically

    let resource_b: Destructible = Destructible::new(String::from("Resource B"));
    println!("Dropping Destructible prematurely.");
    mem::drop(resource_b);
}

While lifetimes control the validity of references, they do not determine when drop() is called.

Xojo

[edit]

Destructors in Xojo (REALbasic) can be in one of two forms. Each form uses a regular method declaration with a special name (with no parameters and no return value). The older form uses the same name as the Class with a ~ (tilde) prefix. The newer form uses the name Destructor. The newer form is preferred because it makes refactoring the class easier.

Class Foobar
  // Old form
  Sub ~Foobar()
  End Sub

  // New form
  Sub Destructor()
  End Sub
End Class

See also

[edit]

References

[edit]
  1. ^ "dtor". TheFreeDictionary.com. Retrieved 2018-10-14.
  2. ^ a b c d e Sebesta, Robert W. (2012). ""11.4.2.3 Constructors and Destructors"". Concepts of Programming Languages (print) (10th ed.). Boston, MA, USA: Addison-Wesley. p. 487. ISBN 978-0-13-139531-2.
  3. ^ "Finalizers (C# Programming Guide)".
  4. ^ Constructors and Destructors, from PHP online documentation
  5. ^ "3. Data model — Python 2.7.18 documentation".
  6. ^ "3. Data model — Python 3.10.4 documentation".
  7. ^ "Destructors - the Rust Reference".
  8. ^ GotW #47: Uncaught exceptions Accessed 31 July 2011.
  9. ^ Smith, Richard; Voutilainen, Ville. "P0593R6:Implicit creation of objects for low-level object manipulation". open-std.org. Retrieved 2022-11-25.
  10. ^ Scott Meyers: Effective Modern C++, O'REILLY, ISBN 9781491903995
  11. ^ C "destructor" function attribute
  12. ^ Erickson, Jon (2008). Hacking the art of exploitation. No Starch Press. ISBN 978-1-59327-144-2.
  13. ^ Bloch, Joshua (2018). Effective Java (3rd ed.). Addison-Wesley. pp. 29–31. ISBN 978-0134685991.