Destructor (computer science)

From Wikipedia, the free encyclopedia

Jump to: navigation, search

In object-oriented programming, a destructor (sometimes shortened to dtor) is a method which is automatically invoked when the object is destroyed. Its main purpose is to clean up and to free the resources which were acquired by the object along its life cycle and unlink it from other objects or resources invalidating any references in the process. The use of destructors is key to the concept of RAII.

In a language with an automatic garbage collection mechanism, it is impossible to deterministically ensure the invocation of a destructor, and hence these languages are unsuitable for RAII. In such languages, unlinking an object from existing resources should be done by an explicit call of appropriate function (usually called Dispose). This method is also recommended for freeing resources rather than using Finalizers for that.

Contents

A virtual destructor is a destructor that can be overridden by subclasses. In C++, use of virtual destructors in inheritance hierarchies facilitates proper clean-up of objects, preventing resource leaks and heap corruption.

In c++, the destructor function is the same name as the class, but with a tilde (~) in front of it. If the object was created locally, its destructor is automatically called, and if the object was created with the new keyword, then its destructor is called when the pointer that points to the object is delete[]'d. This particular class holds a_pointer to a list of character strings. A destructor is required when dynamically created data elements where used, files where opened, locks have to be unlocked or a copy constructor was used.

class myclass {
 public:
   char** a_pointer;   
   void newstring(void)
   {   a_pointer[current] = new char[90];
       current++;
   }

   ~myclass()      // THIS IS THE DESTRUCTOR FUNCTION
   {  int n=0;
      while (n <= current)
      {   delete [] a_pointer[n++];    
      }
   }

 private:
   int current;
 };

Destructors in 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 itself with a ~ (tilde) prefix. The newer form uses the name "Destructor." The newer form is the preferred one because it makes refactoring the class easier.

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

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


Advanced Search
Included Web Search Engines


Safe Search

close

Top Matching Results

Occasionally Search.com will highlight specialized results that are based on the context of your query. Examples of specialized results include specific links to news, images, or video.

Top Matching Results may highlight information from other Search.com pages, content from the CNET Network of sites, or third party content. The listings are based purely on relevance. Search.com does not receive payment for listings in this section but our partners that provide this data may get paid for listing these products.

Sponsored Links

This section contains paid listings which have been purchased by companies that want to have their sites appear for specific search terms and related content. These listings are administered, sorted and maintained by a third party and are not endorsed by Search.com.

Search Results

Search.com sends your search query to several search engines at one time and integrates the results into one list which has been sorted by relevance using Search.com's proprietary algorithm. You can customize the list of search engines included in your metasearch from the preferences.

The search engines that are used in your metasearch may allow companies to pay to have their Web sites included within the results. To view the Paid Inclusion policy for a specific search engine, please visit their Web site. Search.com does not accept payment or share revenue with any search engine partner for listings in this section.