Solved

dispose and finalize methods

Posted on 2006-07-07
4
212 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

752 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