Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

dispose and finalize methods

Posted on 2006-07-07
4
Medium Priority
?
216 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 500 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 500 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

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 response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

972 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