Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does ASP.Net call Dispose on the Page/Controls in a page, or must I do this?

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?

like image 273
Rory Becker Avatar asked Jul 28 '09 08:07

Rory Becker


People also ask

Is disposed a ASP.NET page event?

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.

Do I need to call Dispose C#?

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.

Does .NET GC Call Dispose?

The GC does not call Dispose , it calls your finalizer (which you should make call Dispose(false) ).


2 Answers

It's done for you. Look at UnloadRecursive() from System.Web.UI.Control in Reflector, which is called by ProcessRequestCleanup().

like image 135
Duncan Smart Avatar answered Oct 21 '22 20:10

Duncan Smart


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.

like image 42
Henk Holterman Avatar answered Oct 21 '22 21:10

Henk Holterman