Solved

dispose and finalize methods

Posted on 2006-07-07
4
210 Views
Last Modified: 2010-04-24
Hello, I was wondering what are the finalize() and dispose() methods provided by the .NET and what exactly do they do? What is their relationship with the garbage collector?

Thanks
0
Comment
Question by:yattias
4 Comments
 
LVL 15

Accepted Solution

by:
lakshman_ce earned 125 total points
ID: 17061340
A Dispose method performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.You can override Dispose method to clean up the resources

A Finalize method acts as a safeguard to clean up resources in the event that your Dispose method is not called. You should only implement a Finalize method to clean up unmanaged resources. You should not implement a Finalize method for managed objects, because the garbage collector cleans up managed resources automatically.

The garbage collector tracks and reclaims objects allocated in managed memory. Periodically, the garbage collector performs garbage collection to reclaim memory allocated to objects for which there are no valid references. Garbage collection happens automatically when a request for memory cannot be satisfied using available free memory

Garbage collection consists of the following steps:
The garbage collector searches for managed objects that are referenced in managed code.
The garbage collector attempts to finalize objects that are not referenced.
The garbage collector frees objects that are not referenced and reclaims their memory.

0
 
LVL 48

Assisted Solution

by:AlexFM
AlexFM earned 125 total points
ID: 17061401
Notice that C++/CLI has different syntax for Dispose and Finalize methods. Dispose method syntax: ~MyClass(), finalizer: !MyClass(). delete operator is used to call Dispose method, and finalizer is never called directly. Useful links:

http://blogs.thinktecture.com/cnagel/archive/2006/04/10/414477.aspx
http://www.codeproject.com/managedcpp/cppclidtors.asp
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

837 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question