views:

60

answers:

3

Hi,

I've written a class as follows. I've written it taking into consideration that its mainly for use on the web (i.e. it will be used from aspx pages).

public class TestHelper
{    
    public TestHelper()
    {
        HttpContext ctxt = HttpContext.Current;
        IHttpHandler RequestHandler = ctxt.Handler;
        Page CurrentPage;
        CurrentPage = (Page)RequestHandler;
        CurrentPage.Unload += new EventHandler(CurrentPage_Unload); 
        Debug.Print("Open all connection here...");
    }

    void CurrentPage_Unload(object sender, EventArgs e)
    {
        Debug.Print("Close all connection here...");
    }
}

And I've written my aspx page's code behind like this:

public partial class _Default : System.Web.UI.Page 
{    
    protected void Page_Load(object sender, EventArgs e)
    {
        TestHelper helper = new TestHelper();
        helper = null;
    }

}

In spite of assigning helper to 'null' I find that CurrentPage_Unload() gets executed. Why is this behaviour so? What is this behaviour generally called?

The reason why I've written the class in that style was because I thought I could best manage my db connections in a central fashion in the class. Usually people would call methods on the object like helper.IsValid() followed by helper.ProfileExists() in the aspx code behind. Each of those methods would have their own db connection objects(IDbConnection), and their corresponding Open() & Close() calls to open/close db connection. I just felt that we should only do this only once in code. Hence I used the constructor to open db connections, and the Page object's unload event to close the connection objects. Are there any pitfalls to writing classes this way?

+2  A: 

Assigning null to a variable does not end its actual lifetime. Because .NET uses a non-deterministic garbage collection system (where objects are periodically purged based on several criteria, rather than as soon as they fall out of scope), you cannot rely on an object ever being collected before the process that created it ends.

Furthermore, because you're attaching an event handler (meaning a delegate, which includes a reference to your newly-constructed instance) to another object, you're extending the viable lifetime of your object as well. As long as that event handler is attached, your object cannot be collected until the object it's attached to is eligible.

Adam Robinson
Because the page's unload is wired to a method of the class I am instantiating will the GC actually collect that instance of the class?
deostroll
@deostroll: That's what I'm trying to get at in the second paragraph. Attaching your object (`this`) to the `Unload` event of `CurrentPage` (a reference that exists outside of your class, as it's coming from `RequestHandler`) means that a reference to `this` is being stored as part of the event handler on `RequestHandler`. This means that your object is not eligible for garbage collection until either the object referred to at `RequestHandler` is eligible or you detach from the event.
Adam Robinson
+1. @Adam: I happened to notice that late.
deostroll
+4  A: 

What is happening is you're attaching a delegate to the Unload event of the page. Even after your variable has been set to null, your page still exists and still has an Unload event which still holds a reference to the delegate you added to it.

To remove a delegate you need to use -= syntax.

CurrentPage.Unload -= new EventHandler(CurrentPage_Unload); 
Greg B
A: 

You have to think about what happens when you execute the line

helper = null;

You simply have a reference to an object. When you assign that object to null, you're simply setting your reference to null. Nothing has happened to the object. Consider the following code:

var first = new object();
second = first;

first = null;

Would second now be null? You can think of a reference as simply a number - it's simply the address of the object in memory.

The .NET garbage collector simply looks at objects and checks if there are any references to that object. If not, it will remove that object.

Jaco Pretorius