Wednesday, October 26, 2005

Never Call Dispose() Inside Finalize()

This is regarding a "SMALL" mistake made by our client.

Usually Dispose () method gives us, the developer, the control of disposing unmanaged resources. Calling a Dispose () of a user control, inside a Finalize () method in another class always adds unpredictability to the application. When we have a Finalize() method in a class, even though an object of this class is available for Garbage Collection , it won't be collected - but will be promoted to the next generation.

If you have implemented both the Dispose () and the Finalize () in a certain class, you can avoid Finalize () being called by putting GC.SupressFinalize (me) inside the Dispose () method.

No comments:

Post a Comment