HOME

TheInfoList



OR:

In
object-oriented programming Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data and code. The data is in the form of fields (often known as attributes or ''properties''), and the code is in the form of ...
, a destructor (sometimes abbreviated dtor) is a method which is invoked mechanically just before the memory of the
object Object may refer to: General meanings * Object (philosophy), a thing, being, or concept ** Object (abstract), an object which does not exist at any particular time or place ** Physical object, an identifiable collection of matter * Goal, an ...
is released. It can happen when its lifetime is bound to
scope Scope or scopes may refer to: People with the surname * Jamie Scope (born 1986), English footballer * John T. Scopes (1900–1970), central figure in the Scopes Trial regarding the teaching of evolution Arts, media, and entertainment * Cinema ...
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. Use of destructors is needed for the process of 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 (called
finalizer In computer science, a finalizer or finalize method is a special method that performs finalization, generally some form of cleanup. A finalizer is executed during object destruction, prior to the object being deallocated, and is complementary t ...
s in this case) unsuitable for most purposes. In such languages, the freeing of resources is done either through a lexical construct (such as try..finally, Python's "with" or Java's "try-with-resources"), which is the equivalent to RAII, or explicitly by calling a function (equivalent to explicit deletion); in particular, many object-oriented languages use the Dispose pattern.


Destructor syntax

* C++: destructors have the same name as the class with which they are associated, but with a
tilde The tilde () or , is a grapheme with several uses. The name of the character came into English from Spanish, which in turn came from the Latin '' titulus'', meaning "title" or "superscription". Its primary use is as a diacritic (accent) i ...
(~) prefix. * D: destructors are declared with name ~this() (whereas constructors are declared with this()). * Object Pascal: destructors have the keyword destructor and can have user-defined names, but are mostly named Destroy. * Objective-C: the destructor method has the name dealloc. *
Perl Perl is a family of two high-level, general-purpose, interpreted, dynamic programming languages. "Perl" refers to Perl 5, but from 2000 to 2019 it also referred to its redesigned "sister language", Perl 6, before the latter's name was offic ...
: the destructor method has the name DESTROY; in the Moose object system extension, it is named DEMOLISH. * PHP: In PHP 5+, the destructor method has the name __destruct. There were no destructors in prior versions of PHP.Constructors and Destructors
from PHP online documentation
* Python: there are __del__ methods called destructors by the Python 2 language guide, but they are actually
finalizer In computer science, a finalizer or finalize method is a special method that performs finalization, generally some form of cleanup. A finalizer is executed during object destruction, prior to the object being deallocated, and is complementary t ...
s as acknowledged in Python 3. *
Rust Rust is an iron oxide, a usually reddish-brown oxide formed by the reaction of iron and oxygen in the catalytic presence of water or air moisture. Rust consists of hydrous iron(III) oxides (Fe2O3·nH2O) and iron(III) oxide-hydroxide (FeO( ...
: the destructor method for rust has the name drop * Swift: the destructor method has the name deinit.


In C++

The destructor has the same name as the class, but with a
tilde The tilde () or , is a grapheme with several uses. The name of the character came into English from Spanish, which in turn came from the Latin '' titulus'', meaning "title" or "superscription". Its primary use is as a diacritic (accent) i ...
(~) before it. For example, a class called 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 __NOTOC__ In computer programming, an automatic variable is a local variable which is allocated and deallocated automatically when program flow enters and leaves the variable's scope. The scope is the lexical context, particularly the function or ...
, 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 statement (dynamically on the heap), then its destructor is called when the 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.


Example

#include #include class Foo ; int main() Objects which cannot be safely copied and/or assigned should be disabled from such semantics by declaring their corresponding functions as deleted within a public encapsulation level. 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.").


In C with GCC extensions

The
GNU Compiler Collection The GNU Compiler Collection (GCC) is an optimizing compiler produced by the GNU Project supporting various programming languages, hardware architectures and operating systems. The Free Software Foundation (FSF) distributes GCC as free softwar ...
'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.


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 In computer programming and software design, code refactoring is the process of restructuring existing computer code—changing the '' factoring''—without changing its external behavior. Refactoring is intended to improve the design, structu ...
the class easier. Class Foobar // Old form Sub ~Foobar() End Sub // New form Sub Destructor() End Sub End Class


See also

*
Finalizer In computer science, a finalizer or finalize method is a special method that performs finalization, generally some form of cleanup. A finalizer is executed during object destruction, prior to the object being deallocated, and is complementary t ...
* Constructor (computer science) *
Object lifetime In object-oriented programming (OOP), the object lifetime (or life cycle) of an object is the time between an object's creation and its destruction. Rules for object lifetime vary significantly between languages, in some cases between implementa ...
* Resource Acquisition Is Initialization


References

{{Reflist Method (computer programming) C++