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 to malloc() and free(). • include • include typedef struct { void* data; // Some resource, could be anything size_t size; // Size of the resource } Resource; // The "constructor" Resource* createResource(size_t size) { Resource* res = (Resource*)malloc(sizeof(Resource)); if (!res) { fprintf(stderr, "Failed to allocate memory for resource.\n"); return NULL; } res->data = malloc(size); if (!res->data) { fprintf(stderr, "Failed to allocate memory for resource data.\n"); free(res); return NULL; } res->size = size; return res; } // The "destructor" void destroyResource(Resource* res) { if (res) { if (res->data) { free(res->data); } free(res); } } int main() { // Allocate the resource of 50 bytes Resource* myResource = createResource(50); if (!myResource) { return 1; } for (size_t i = 0; i size; ++i) { ((char*)myResource->data)[i] = (char)(i % 256); // Just some dummy data } printf("First 10 bytes of resource data: "); for (size_t i = 0; i data)[i]); } printf("\n"); // Free the resource destroyResource(myResource); }
GCC extensions The
GNU Compiler Collection's
C compiler comes with 2 extensions that allow implementing destructors: • The destructor function attribute 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. • The
cleanup variable attribute allows attaching a destructor function to a variable: the function is called when the variable goes out of scope.
C++ The destructor has the same name as the class, but with a
tilde () before it. 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() { 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.). 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 { private: size_t length; char* data; friend struct formatter; public: // Constructor explicit Foo(const char s[] = ""): length{std::strlen(s)}, data{new char[length + 1]} { std::strcpy(data, s); } Foo(const Foo&) = delete("Copy construction disabled"); Foo& operator=(const Foo&) = delete("Copy assignment disabled"); // Destructor ~Foo() { delete[] data; } }; template <> struct std::formatter { // Definition of Formatter for Foo here }; 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, 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 { private: size_t length; unique_ptr data; friend struct formatter; public: // Constructor explicit Foo(const char s[] = ""): length{std::strlen(s)}, data{std::make_unique(length + 1)} { std::strcpy(data.get(), s); } Foo(const Foo&) = delete("Copy construction disabled"); Foo& operator=(const Foo&) = delete("Copy assignment disabled"); // Destructor is automatically handled by unique_ptr ~Foo() = default; }; template <> struct std::formatter { // Definition of Formatter for Foo here }; int main(int argc, char* argv[]) { Foo foo("Hello from the stack!"); std::println("{}", foo); unique_ptr foo = std::make_unique("Hello from the heap!"); std::println("{}", *foo); }
C# 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. namespace Wikipedia.Examples; using System; using System.Collections.Generic; using System.Data.SqlClient; public record User(int Id, string Name, string Email); public class UserUnitOfWork : IDisposable { private readonly SqlConnection _connection; private SqlTransaction _transaction; private bool _disposed; public UserUnitOfWork(string url) { _connection = new(url); _connection.Open(); _transaction = _connection.BeginTransaction(); } public List GetActiveUsers() { // ... } public void Commit() { _transaction?.Commit(); } public void Dispose() { if (_disposed) { return; } try { _transaction?.Dispose(); _connection?.Close(); _connection?.Dispose(); } finally { _disposed = true; } } } class Program { static void Main(string[] args) { using (UserUnitOfWork uow = new(/* connection string */) { Console.WriteLine("Using UserUnitOfWork..."); List users = uow.GetActiveUsers(); uow.Commit(); // ... } // Connection, transaction closed here } } Destructors in
C# are not manually called or called by a delete operator like in C++. They are only called by the garbage collector. However, finalizers should only be used when absolutely necessary, such as handling unmanaged resources like
pointers and native handles. This is because adding a finalizer moves the object to the finalization queue and requires at least two garbage collection cycles, which slows performance and increases memory pressure. namespace Wikipedia.Examples; using System; class NativeResourceHolder : IDisposable { private IntPtr _nativeHandle; private bool _disposed; public NativeResourceHolder() { _nativeHandle = AllocateNative(); } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } // The Finalizer ~NativeResourceHolder() { Dispose(false); } protected virtual void Dispose(bool disposing) { if (!_dipsosed) { // Free managed resources here, if any } FreeNative(_nativeHandle); _disposed = true; } private IntPtr AllocateNative() => new IntPtr(42); private void FreeNative(IntPtr ptr) { /* ... */ } } class Program { static void Main(string[] args) { NativeResourceHolder obj = new(); GC.Collect(); GC.WaitForPendingFinalizers(); Console.WriteLine("Program finished"); } }
D The following demonstrates the usage of a destructor in
D. import std.stdio; struct Integer { int x; this(int value) { x = value; writeln("Constructor called with x = ", x); } ~this() { writeln("Destructor called for x = ", x); } } void main() { { Integer s = Integer(10); writeln("Inside scope"); } // Destructor runs here writeln("Outside scope"); }
Java In Java there are no true "destructors" like C++ and C#, but Java does provide 2 interfaces that implement a close() method, 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. These clean() methods do not deallocate memory, as memory is reclaimed through garbage collection. package org.wikipedia.examples; import java.sql.*; import java.util.ArrayList; import java.util.List; public record User(int id, String name, String Email) {} class UserUnitOfWork implements AutoCloseable { private final Connection conn; private bool committed = false; public UserUnitOfWork(String url, String username, String password) throws SQLException { this.connection = DriverManager.getConnection(url, username, password); this.connection.setAutoCommit(false); } public List getActiveUsers() throws SQLException { // ... } public void commit() throws SQLException { connection.commit(); committed = true; } @Override public void close() throws SQLException { try { if (!committed) { connection.rollback(); } } finally { connection.close(); } } } public class Example { public static void main(String[] args) { try (UserUnitOfWork uow = new UserUnitOfWork(/* URL and credentials */)) { System.out.println("Using UserUnitOfWork..."); List users = uow.getActiveUsers(); uow.commit(); // ... } // after try-with-resources, d.close() will be called } } Prior to Java 7, a "try-finally" block was used, where the d.close() call was put inside the finally block. 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. package org.wikipedia.examples; 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). Much like Closeable and AutoCloseable, it does not reclaim memory, which is instead done by garbage collection. package org.wikipedia.examples; 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. package org.wikipedia.examples; 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 queue = new ReferenceQueue<>(); PhantomReference phantomRef = new PhantomReference<>(resource, queue); resource = null; System.gc(); Reference ref = queue.poll(); if (ref != null) { System.out.printf("Object is ready to be collected: %s%n", ((PhantomReference)ref).get()); } } } 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.
Python 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) -> None: self.name: str = 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 Rust does not have destructors in the sense of object-oriented programming, but a struct can implement the std::ops::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."); } // While
lifetimes control the validity of references, they do not determine when drop() is called.
TypeScript Although TypeScript does not have manual memory management, it has resource management similar to using-with-resource blocks in
C# or try-with-resources blocks in
Java, or
C++ resource acquisition is initialization, that automatically close resources without need for finally blocks. In TypeScript, to automatically close an object, it must implement a global interface Disposable, and implement a method Symbol.dispose(). This will automatically be called at the end of scope. import * as fs from 'fs'; class TempFile implements Disposable { #path: string; #handle: number; constructor(path: string) { this.#path = path; this.#handle = fs.openSync(path, "w+"); } write(data: string): void { fs.writeSync(this.#handle, data); } [Symbol.dispose](): void { fs.closeSync(this.#handle); fs.unlinkSync(this.#path); } } export function doSomeWork() { using file: TempFile = new TempFile(".some_temp_file.txt"); if (someCondition()) { // do something here } }
Xojo 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==