Solved

dispose and finalize methods

Posted on 2006-07-07
4
208 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now