Dispose
Encyclopedia
In computer programming
Computer programming
Computer programming is the process of designing, writing, testing, debugging, and maintaining the source code of computer programs. This source code is written in one or more programming languages. The purpose of programming is to create a program that performs specific operations or exhibits a...

, the dispose pattern is a design pattern
Design pattern (computer science)
In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design. A design pattern is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that...

 which is used to handle resource cleanup in runtime environments that use automatic garbage collection. The fundamental problem that the dispose pattern aims to solve is that, because objects in a garbage-collected environment have finalizer
Finalizer
In object-oriented programming languages that use garbage collection, a finalizer is a special method that is executed when an object is garbage collected. It is similar in function to a destructor...

s rather than destructors
Destructor (computer science)
In object-oriented programming, a destructor is a method which is automatically invoked when the object is destroyed...

, there is no guarantee that an object will be destroyed at any deterministic point in time—a guarantee that is necessary for some other resource management patterns, such as Resource Acquisition Is Initialization
Resource Acquisition Is Initialization
Resource Acquisition Is Initialization is a programming idiom used in several object-oriented languages like C++, D and Ada. The technique was invented by Bjarne Stroustrup to deal with resource deallocation in C++...

. The dispose pattern works around this by giving an object a method
Method (computer science)
In object-oriented programming, a method is a subroutine associated with a class. Methods define the behavior to be exhibited by instances of the associated class at program run time...

 (usually called Dispose or something similar) which frees any resources the object is holding onto. Unless the language offers an alternative, this method must be manually invoked by clients that use the object once they are finished with it.

Examples

Fundamentally, the pattern looks like this from the caller's point-of-view:


// Acquire the resource.
Resource resource = null;
try {
resource = getResource;
// Perform actions with the resource.
...
} finally {
if (resource != null) {
// Free the resource.
resource.Dispose;
}
}


The try...finally construct is necessary for proper exception safety—if the code that uses the resource throws an exception, the resource will not be freed unless the Dispose call is in a finally block. Also, the check against null is necessary if the object representing the resource is not directly instantiated, but rather is obtained from some other source that could return null.

To make this pattern less verbose, several languages have some kind of built-in support for it. For example, C# features the using statement, which automatically and safely calls the Dispose method on an object that implements the IDisposable interface
Interface (computer science)
In the field of computer science, an interface is a tool and concept that refers to a point of interaction between components, and is applicable at the level of both hardware and software...

:


using (Resource resource = GetResource)
{
// Perform actions with the resource.
...
}


Python
Python (programming language)
Python is a general-purpose, high-level programming language whose design philosophy emphasizes code readability. Python claims to "[combine] remarkable power with very clear syntax", and its standard library is large and comprehensive...

's with statement can be used to similar effect:

with get_resource as resource:
# Perform actions with the resource.
...
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK