I am aware that all objects which implement IDisposable
should be disposed of as soon as they are no longer needed in order to free the memory used by their unmanaged resources.
My question relates to objects which I know for a fact will live until the host process itself is terminated. Would it make any difference if I dispose of them or not? Is there any chance of memory not being freed when the process dies? What about GDI objects? Would the GDI handles be freed when the process dies even if they were not disposed?
I fully understand that it is good practice to dispose of all objects anyways. I ask purely out of curiosity.
It depends on the object (resource) in question.
When a process terminates all unmanaged memory, filehandles and other OS resources will be released, even if the associated finalizers failed to run.
But I'm not so sure about db handles, named-mutexes etc.
So before you could consider it safe to not call Dispose, you would have to know about the resource type and how it relates to the process. Much easier to just call Dispose() out of general principle.
But it is a theoretical argument, most classes will use SafeHandle : CriticalFinalizerObject
. So I don't think it ever is a real practical problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With