views:

322

answers:

3

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?

+5  A: 

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.

Henk Holterman
I don't doubt you're correct. I'm trying to convince someone else though and would appreciate any references you might know of that would support this.
Rory Becker
I think the best evidence in this case is the lack of articles about how to clean up your controls (-: But I'll edit a little.
Henk Holterman
Henk is correct. Neither you nor "someone else" has seen any article from Microsoft on ASP.NET that has any "dispose loop". That's because it's not needed.
John Saunders
However, it's a flaw that documentation on this method is lacking. The idea of ownership is fairly general, but it's not always clear who's the owner - the docs should include such information as a matter of course for all IDisposables.
Eamon Nerbonne
+2  A: 

This article on The ASP.NET Page Life Cycle states that:

"Unload is called after the page has been fully rendered, sent to the client, and is ready to be discarded. At this point, page properties such as Response and Request are unloaded and any cleanup is performed."

I would take that "any cleanup" means disposal of controls etc. I can't imagine that the designers of the ASP.NET framework would have overlooked that and nobody would have noticed.

Dan Diplo
+6  A: 

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

Duncan Smart
Thx Duncan, that's the first real evidence I've seen.
Rory Becker