When will the disposal method not be called?

I read this article the other day and wondered why Finalizer came along with the Dispose method. I read here about why you can add Dispose to Finalizer. My curiosity is when will Finalizer be called using Dispose? Is there any sample code, or is it based on something that happens on the system where the software runs? If so, it may happen that the Dispose method is not started by the GC.

+6
garbage-collection c # idisposable dispose finalizer
source share
5 answers

The finalizer's goal here is just a warning against memory leaks (unless you need to explicitly call Dispose ). It also means that you do not need to delete objects if you want them to free resources when the program shuts down, since the GC will forcefully terminate and collect all objects anyway.

As a connected point, it is important to dispose of the object in a slightly different way when it is done from the finalizer.

 ~MyClass() { Dispose(false); } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected void Dispose(disposing) { if (!this.disposed) { if (disposing) { // Dispose managed resources here. } // Dispose unmanaged resources here. } this.disposed = true; } 

The reason you don’t want to remove managed resources in your finalist is that you will actually create strong links for them to them, and this may prevent the GC from doing the job correctly and collecting them. Unmanaged resources (e.g. Win32 descriptors, etc.) should always be explicitly closed / deleted, of course, since the CLR does not know about them.

+9
source share

This is mainly to protect yourself. You cannot dictate what the end user of your class will do. By providing a finalizer in addition to the Dispose method, the GC will "Dispose" your object, freeing your resources accordingly, even if the user forgets to call Dispose () or uses your class incorrectly.

+4
source share

The trapper is called when an object collects garbage. Disposal must be explicitly called for. The following code will call the finalizer, but the Dispose method is missing.

 class Foo : IDisposable { public void Dispose() { Console.WriteLine("Disposed"); } ~Foo() { Console.WriteLine("Finalized"); } } ... public void Go() { Foo foo = new Foo(); } 
+2
source share

The dispose method must be explicitly called either by calling Dispose () or by the presence of an object in the using statement. The GC will always call the finalizer, so if there is anything that needs to happen before the objects are deleted, the finalizer should at least check to make sure everything in the object is cleared.

You want to avoid cleaning objects in the finalizer, if at all possible, because it causes additional work compared to deleting them before hand (for example, calling dispose), but you should always at least check the finalizer if there are objects lying around which need to be removed.

+1
source share

An important but subtle note that has not yet been mentioned: the rarely used goal of Dispose is to prevent premature cleaning of the object. Objects with finalizers must be carefully written so that the finalizer does not execute earlier than expected. The finalizer cannot start before the last method call that will be executed on the object (*), but sometimes it can run during the last method call if the object is left after the method is completed. Code that correctly Object utility cannot refuse an object before calling Dispose, therefore there is no danger for the finalizer destroying the code that correctly uses Dispose. On the other hand, if the last method of using an object uses objects that will be cleared in the finalizer after its last use of the object reference itself, it is possible for the garbage collector to call Finalize on the object and to clear which are still in use. The safeguard is that any invocation method that uses objects that will be cleared by the finalizer must at some point follow a method invocation that uses "this". GC.KeepAlive (this) is a good method for this.

(*) Non-virtual methods that extend to inline code that does nothing with the object can be exempted from this rule, but Dispose usually is or calls a virtual method.

0
source share

All Articles