17

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?

H H
  • 263,252
  • 30
  • 330
  • 514
Rory Becker
  • 15,551
  • 16
  • 69
  • 94

4 Answers4

9

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

Duncan Smart
  • 31,172
  • 10
  • 68
  • 70
5

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.

H H
  • 263,252
  • 30
  • 330
  • 514
  • 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 Jul 28 '09 at 08:13
  • 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. – H H Jul 28 '09 at 08:28
  • 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 Jul 28 '09 at 10:28
  • 2
    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 Jul 30 '09 at 07:57
2

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
  • 25,076
  • 4
  • 67
  • 89
1

Interpreted differently, there's more complexity to this question than meets the eye.

Sure Disposed gets called, but does it do anything? It depends.

If you've subscribed to the Disposed event of a page or control and are banking on it being called per-request, you might be in for a surprise. Yes, technically speaking ProcessRequestCleanup() calls it for you, but have a look at what it actually calls:

public virtual void Dispose()
{
    IContainer service = null;
    if (this.Site != null)
    {
        service = (IContainer) this.Site.GetService(typeof(IContainer));
        if (service != null)
        {
            service.Remove(this);
            EventHandler handler = this.Events[EventDisposed] as EventHandler;
            if (handler != null)
            {
                handler(this, EventArgs.Empty);
            }
        }
    }
    if (this._occasionalFields != null)
    {
        this._occasionalFields.Dispose();
    }
}

Without a design surface, this code essentially does nothing at run-time, meaning your Disposed handlers will never execute.

Lesson is don't rely on Disposed handlers to execute per request. You can override it to guarantee something executes, but Unloaded is a much safer bet.

Nariman
  • 6,368
  • 1
  • 35
  • 50