Given that the Control class implements IDisposable, I would think that ASP.Net is at least capable of triggering a Dispose cascade as the Page finishes it's life-cycle on the way out the door to the browser?
Simple question: Is this the case, or must I do this?
Disposed event Occurs when a server control is released from memory, which is the last stage of the server control lifecycle when an ASP.NET page is requested. Control. Unload Event Occurs when the server control is unloaded from memory. Server controls must perform any final clean-up(Control.
Rule of thumb: if a class implements IDisposable you should always call the Dispose method as soon as you have finished using this resource. Even better wrap it in a using statement to ensure that the Dispose method will be called even if an exception is thrown: using (var reader = conn.
The GC does not call Dispose , it calls your finalizer (which you should make call Dispose(false) ).
It's done for you. Look at UnloadRecursive()
from System.Web.UI.Control
in Reflector, which is called by ProcessRequestCleanup()
.
No, you should not call Dispose on controls, that is being done. You are responsible for other Disposable objects you create outside the Control structure (FileStreams etc).
This follows from a general .NET principle: The Page is the owner of the Controls and therefore required to cascade the (explicit) Dispose to them. For the actual code you will have to Reflector the code for Web.UI.Control.
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