Disposing of Objects with long living dependencies?

I read this on MSDN : Because the Dispose method must be called explicitly, objects that implement IDisposable must also implement a finalizer to handle freeing resources when Dispose is not called By default, the garbage collector automatically calls an object's finalizer prior to reclaiming its memory. However, once the Dispose method has been called, it is typically unnecessary for the garbage collector to call the disposed object's finalizer. To prevent automatic finalization, Dispose implementations can call the GC.

SuppressFinalize method So to be on the safe side in this case, I'd implement both IDisposable and a finalizer. And I agree, it's a bit dirty, but then again, that's the price you pay when dealing with long-lifespan objects.

I read this on MSDN: Because the Dispose method must be called explicitly, objects that implement IDisposable must also implement a finalizer to handle freeing resources when Dispose is not called. By default, the garbage collector automatically calls an object's finalizer prior to reclaiming its memory. However, once the Dispose method has been called, it is typically unnecessary for the garbage collector to call the disposed object's finalizer.To prevent automatic finalization, Dispose implementations can call the GC.

SuppressFinalize method. So to be on the safe side in this case, I'd implement both IDisposable and a finalizer. And I agree, it's a bit dirty, but then again, that's the price you pay when dealing with long-lifespan objects.

1 However, a finaliser is only required if we're using unmanaged code. – Ray Booysen Feb 16 at 10:16.

The obvious solution to this, after leaving this question alone for a year, is that I should implement IDisposable and simply unsubscribe the event in my Dispose(). Since I'm not responsible for the lifetime of the dependency, it is not up to me to do anything with it.

Let us say that an instance of ABC is a long living object and that my dependency is an even longer running object. When an instance of ABC needs to be cleaned up, I have two options. One I could have a Cleanup() method to unsubscribe from the ANewEvent event or I could implement IDisposable and in the Dispose unwire the event.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions