The Dangers of Data Binding (part 3456 of infinity)…

Uncategorized

So, when you have a form, it’s nice to bind data to the controls. In spite of my personal biases, there is nothing wrong with that.

But, you have to be careful about what you are binding to – if the object that you bind to the control does not go out of scope, the control will not, either. So, you will have a memory leak. The fun thing about this kind of memory leak is it will also leak graphics handles, which are a finite resource on a machine, and so if you have a lot of controls on the form, you may actually crash the application fairly easily, even thought there is memory to spare.

Here is an example of where this kind of data binding problem happened:

   1: uxComboBoxPackageTypes.DataSource = new BindingSource

   2:     (PackageTypeDictionary.PackageTypes, null);

Note that PackageTypeDictionary.PackageTypes is a static dictionary. It will never go out of scope, as long as the application is running. So, in this case, every Med Detail screen that gets popped up in Visual Assign will stay around forever.

The solution, in this case, is to upon form closing, set the DataSource to null, to break the binding:

   1: protected override void OnClosed(EventArgs e)

   2: {

   3:     base.OnClosed(e);

   4:  

   5:

   6:  

   7:     uxComboBoxPackageTypes.DataSource = null;

   8: }

To find this, I used YourKit (which helped me pin down what was leaking in the overall scheme) and WinDbg (where I got into the gritty details). There are lots of good resources on WinDbg out there, but I want to point out this excellent article which really helped me figure out how to track down event chain leakages (and led to figuring out where the real problem lie):

http://blogs.msdn.com/b/tess/archive/2006/01/23/net-memory-leak-case-study-the-event-handlers-that-made-the-memory-baloon.aspx

Advertisements

Pgh Code Camp 2010.2

Uncategorized

Its getting to be that time. Wait, what time? We always have Code Camp in the spring – the leaves are going to fall (or started falling, if your trees are lame like mine) – surely Eric is on some sort of hallucinogens. Nope. This year we’re doing Code Camp twice – Code Camp 2010.2 will be October 16, 2010, once again in Pitt’s compsci building. Details can be found here.

Our call for speakers has gone out a while back, but there’s still room for more, so if you want to present on something, there’s still time. You can go fill out the call for speakers form here: http://codecamppgh.wufoo.com/forms/code-camp-20102-call-for-speakers/.