dotNoted

Icon

Observations of .Net development in the wild

Lawrence Lessig on InfoCard

Wow –

Lawrence Lessig has some unusually strong words of appreciation for an idea to come from Redmond.

Not that he is a MS detractor, but I haven’t really seen someone with such a well established aura of independence loose such encomiums on any idea proceeding from Corporate America, let alone from the MotherShip. Obviously, his independence is quite secure – InfoCard is a great concept, regardless of the progenitor.

Filed under: Metathought

Don’t ever do this bad thing

This is bad:
 

protected override void OnLoad( EventArgs e )

{

base.OnLoad(e);

PubsDataSet.Instance.UpdateFailed += new UpdateFailedDelegate(Instance_UpdateFailed);

}

 

As you can see, this is an override from an ASP.Net code behind class. Yep, the .Instance property is a Singleton. I knew it was a no-no to hook up an event listener to a static instance in an Asp.Net solution, because the resulting class which has the listener never gets GC’d, and you get memory leakage in a nasty way.

 

What’s more, if you execute the code above, what happens is that all references to controls on the page return "null" since, due to the way that events are implemented as linked lists, the reference that you are looking at in the event handler is an old instance of the page, which should have been disposed, but isn’t because you have a reference outstanding and the GC won’t collect it.

 

However, knowing this didn’t stop me from doing it in an example I’m cooking up for a course. This is why experience is valuable – you know when something is wrong because you’ve felt the pain of doing it wrong, which lends to a much more acute sense of what is right than just studying it. Knowing the symptoms from studying the above analysis earlier helped me kill this bug in under 30 minutes, but having the experience will prevent me from creating the bug.

 

Here is a good solution which makes use of an Anonymous method to handle the event callback. Notice how the handler is removed when the page is unloaded – this is key:

private UpdateFailedDelegate updateFailed = delegate( object sender, UpdateFailedEventArgs e )

{

EditErrorLabel.Visible =

true;

EditErrorLabel.Text = e.FailureReason.ToString();

};

protected override void OnLoad( EventArgs e )

{

base.OnLoad(e);

PubsDataSet.Instance.UpdateFailed += updateFailed;

}

protected override void OnUnload( EventArgs e )

{

base.OnUnload(e);

PubsDataSet.Instance.UpdateFailed -= updateFailed;

}

 

Filed under: .Net Basics