First of all, I would like to note that IDisposable does not apply only to objects of the Windows kernel or unmanaged memory. Rather, it is something that garbage collection does not understand (of which kernel objects are a special case).
A small example, just to give you an idea:
sealed class Logger : IDisposable { private bool _disposed = false; public Logger() { System.IO.File.AppendAllText( @"C:\mylog.txt", "LOG STARTED" ); } ~Logger() { Dispose(); } public void Dispose() { if ( !_disposed ) { System.IO.File.AppendAllText( @"C:\mylog.txt", "LOG STOPPED" ); _disposed = true; } } public void WriteMessage( string msg ) { System.IO.File.AppendAllText( @"C:\mylog.txt", "MESSAGE: " + msg ); } }
Note that this Logger class does not “hold” all core-type resources. He opens the file and closes it immediately. However, there is such a logic that when destroying an object, it should write "LOG STOPPED". This logic is something that the GC cannot understand - and this is the place to use IDisposable .
Therefore, you cannot expect to clean up Windows kernel objects after you. Perhaps something else does not know Windows.
However, provided that your disposable objects are correctly written (i.e., by calling Dispose in the finalizer), they will still be deleted by the CLR after the process is complete.
So, the answer is: yes, you do not need to call Dispose immediately before the project exit. But NOT because one-time resources are kernel objects (because they are not necessarily there).
Edit
As Josh Einstein correctly pointed out, finalizers are not really guaranteed to work at the output of the process. So then the answer will be: always call Dispose, just to file a lawsuit
Fyodor soikin
source share