A special class method called when an object instance of a class is destroyed. With some languages they are called when the object instance goes out of scope, with some languages you specifically have to call the destructor in code to destroy the object, and others use a garbage collector to dispose of object instances at specific times.
Desctructors are commonly used to free the object instance but with languages that have a garbage collector object instances are disposed of when appropriate. Either way, destructors or their equivalent are commonly used to free up resources allocated in the class constructor.
Languages Focus Are object instances freed with a garbage collector? Or, do you have to destroy object instances.
ASP Classic:
Class_Terminate
When an object instance is destroyed, ASP calls a special parameter-less sub named Class_Terminate . For example, when the variable falls out of scope. Since you cannot specify parameters for this sub, you also cannot overload it.
To explicitly destroy an object, use Set YourClass = nothing . If the Class object is explicitly destroyed, the client returns with the script engine error details.
When an object instance is created from a class, ASP calls a special sub called Class_Initialize .
Syntax Example: Class Cyborg Public Sub Class_Terminate Response.Write "<br>Class destroyed" End Sub End Class
Cross Reference Examples:
Access VBA:
When an object instance is destroyed, Access VBA calls a special parameter-less sub named Class_Terminate . For example, when the variable falls out of scope. Since you cannot specify parameters for this sub, you also cannot overload it.
To explicitly destroy an object, use Set YourClass = nothing .
When an object instance is created from a class, Access VBA calls a special sub called Class_Initialize .
More Info / Comment
C#:
"Finalizer" ~ClassName
In C# you cannot explicitly destroy a managed object. Instead, the .Net Framework's garbage collector (GC) takes care of destroying all objects. The GC destroys the objects only when necessary. Some situations of necessity are when memory is exhausted or you explicitly call the System.GC.Collect() method. In general, you never need to call System.GC.Collect() .
In .Net, a finalizer is used to free non-managed objects such as a file or network resource. In C#, a finalizer is a method with the same name as the class but preceded with a tilde (as in ~ClassName ). The finalizer method implicity creates an Object.Finalize method (you cannot directly call nor override the Object.Finalize method). Because you don't know when the garbage collector will call your finalizer, Microsoft recommends you implement the IDisposable interface for non-managed resources and call it's Dispose() method at the appropriate time.
Syntax Example: class Cyborg { public: //Destructor for class Cyborg. ~Cyborg(); { //Free non-managed resources here. } };
C++:
~ClassName
A member function with the same name as the class prefixed with a ~ (tilde). C++ destructors are automatically called when an object goes out of scope, or when you delete a dynamically allocated object. Every class can have only one destructor.
Syntax Example:
class Cyborg { public: // Constructor for class Cyborg Cyborg(); // Destructor for class Cyborg ~Cyborg(); };
C++/CLI:
"Finalizer" ~ClassName
Unlike standard C++, C++/CLI uses the .Net garbage collector to free managed object instances. Prism does not have nor need a true destructor.
In .Net, a finalizer is used to free non-managed objects such as a file or network resource. Because you don't know when the garbage collector will call your finalizer, Microsoft recommends you implement the IDisposable interface for non-managed resources and call it's Dispose() method at the appropriate time.
More Info / Comment
Corel Paradox:
Not Supported
Delphi:
Free or FreeAndNil
Object Pascal uses a standard virtual destructor named Destroy which is called by the standard Free method. All objects are dynamic, so you need to call MyObject.Free method or the FreeAndNil(MyObject) routine for each object you create.
Syntax Example:
var MyObject: TObject; begin MyObject := TObject.Create; //Use it... MyObject.Free //Or use...FreeAndNil(MyObject); end;
Delphi Prism:
"Finalizer" finalize()
Unlike Delphi, Delphi Prism uses the .Net garbage collector to free managed object instances. Prism does not have nor need a true destructor.
In .Net, a finalizer is used to free non-managed objects such as a file or network resource. In Prism, you use the keyword finalizer to indicate the method is a finalizer. Each class can implement one and only one finalizer with a method name of finalize() , which is the method name used in Java and VB.Net.
Because you don't know when the garbage collector will call your finalizer, For non-managed resources, Microsoft recommends you implement the IDisposable interface and call it's Dispose() method at the appropriate time.
Syntax Example: type Cyborg = class(IDisposable) private disposed: Boolean; method Dispose(disposing: Boolean); public method Dispose; finalizer Finalize; end;
Java:
"finalize" finalize()
Java has a garbage collection algorythm that runs as a background task so it has no destructors. You can use the finalize() method to close additonal resources such as file handles.
Syntax Example:
protected void finalize() throws Throwable { try { close(); // close open files } finally { super.finalize(); } }
VB Classic:
When an object instance is destroyed, VB6 calls a special parameter-less sub named Class_Terminate . For example, when the variable falls out of scope. Since you cannot specify parameters for this sub, you also cannot overload it.
To explicitly destroy an object, use Set YourClass = nothing .
When an object instance is created from a class, VB6 calls a special sub called Class_Initialize .
More Info / Comment
VB.Net:
"Finalizer" Finalize()
In VB.Net you cannot explicitly destroy a managed�object. Instead, the .Net Framework's garbage collector (GC) takes care of destroying all objects. The GC destroys the objects only when necessary. Some situations of necessity are when memory is exhausted or you explicitly call the System.GC.Collect() method. In general, you never need to call System.GC.Collect() .
In .Net, a finalizer is used to free non-managed objects such as a file or network resource. In VB.Net, a finalizer is an overridden sub called Finalize . Because you don't know when the garbage collector will call your finalizer, Microsoft recommends you implement the IDisposable interface for non-managed resources and call it's Dispose() method at the appropriate time.
Syntax Example: Class Cyborg � Protected Overrides Sub Finalize() ��� 'Free non-managed resources here. ��� MyBase.Finalize() � End Sub End Class