Question about IDisposable.

wyrd

Senior Contributor
Joined
Aug 23, 2002
Messages
1,405
Location
California
If your class implements IDisposable, is it guaranteed to be called by the application that it was created in when the application exits? For example, you forgot to Dispose of a class or you have a class that has a static member which is implements IDisposable. In both cases, will the application call Dispose for you when it is exited?
 
IIRC Dispose will not be called automatically when the program exits. however if you override the finalize method in a class this will automatically be called (sometime after the object goes out of scope and before the application terminates).
MS info on the subject...
http://msdn.microsoft.com/library/d.../en-us/cpgenref/html/cpconfinalizedispose.asp

also
http://www.vbinfozine.com/a_disposable.shtml
has an idea or two.

if you are using C# though a new keyword 'using' can be used.
http://msdn.microsoft.com/library/d...ry/en-us/csspec/html/vclrfcsharpspec_8_13.asp

this will call the Dispose method automatically
 
The memory that belongs to managed objects will be released automatically and collected by the garbage collector when it is no longer needed (i.e. when the program exits). Memory for unmanaged resources (such as GDI32 objects) needs to be manually freed, though.
 
Back
Top