Instance has been disposed - where to now?

Use this forum for questions on how to use .NET Memory Profiler and how to analyse memory usage.
Post Reply
Greg

Instance has been disposed - where to now?

Post by Greg » Mon Jan 30, 2006 12:48 am

I have a Form that contains a Panel. And that Panel contains a whole lot of controls, third party datasets and event handlers.
Using the "New or Removed Instances" filter I can see that the form is gone but not the panel. The Panel appears in the "Instance Details" tab as "Instance has been disposed" but it still seems to be taking up memory (16M including children). I have disposed all the datasets and all I have left now are the event handlers. Is it a problem that the event handlers have references back to the panel?
Where should I be looking next to get this panel released? Using Realtime I can see that the Form goes up and down but the Panel just keeps rising on each opening.
BTW, Dispose Info shows empty.

Any help appreciated.

Andreas Suurkuusk
Posts: 1029
Joined: Wed Mar 02, 2005 7:53 pm

Post by Andreas Suurkuusk » Mon Jan 30, 2006 10:04 am

To find out why an instance has not been garbage collected, you should look at the root paths of that instance. The root paths will tell you what's keeping the instance from being garbage collected.

EventHandlers that are not properly removed are often a cause of memory leaks. If you have EventHandlers that reference the Panel, you must make sure that they (or rather the event sources) do not have a longer life-time than the Panel itself. If the event sources have a longer life-time, then it's very important that you manually disconnect the EventHandlers from the Panel.

BTW. What kind of Panel are you talking about, is it the standard System.Windows.Forms.Panel? Or is it a user-defined Panel? You don't usually hook up EventHandlers that reference a standard System.Windows.Forms.Panel.

What do you mean when you say that "Dispose Info shows empty". Don't you see any classes in the Classes list when you select the "Dispose Info" view?
Best regards,

Andreas Suurkuusk
SciTech Software AB

Greg

Post by Greg » Mon Jan 30, 2006 10:24 am

Thanks, Andreas.

I will check out the event sources. Obviously, the EventHandlers that reference the panel are added to an event source. It is the event source I should be chasing. Missed that one.

When I said Panel I meant a UserControl derived panel. Ooops.

When I said "Dispose Info shows empty" I meant that that are no classes in the Dispose Info view.

Andreas Suurkuusk
Posts: 1029
Joined: Wed Mar 02, 2005 7:53 pm

Post by Andreas Suurkuusk » Mon Jan 30, 2006 8:43 pm

We investigated the problem with the "Dispose info" view, and we were able to see the problem you described. It seems like we accidentally introduced a bug in v2.6.74 when redesigning the way classes are reported.

We have now fixed this problem, and you can download a new version from http://memprofiler.com/download.aspx.
Best regards,

Andreas Suurkuusk
SciTech Software AB

Guest

Post by Guest » Wed Feb 01, 2006 12:11 pm

Greg wrote:I will check out the event sources. Obviously, the EventHandlers that reference the panel are added to an event source. It is the event source I should be chasing.
In our experience event handlers, tooltips (and prior to .Net 2.0 MenuItems) are the biggest source of resource leaks we find in our application. One of the faults with the Form Designer is that it adds the code to hook up an event handler, but does nothing to unhook it afterwards. In our view this is a big oversight by Microsoft, but i suppose they are looking at it from the view of a simple app that runs, shows a form, does some UI and then quits.

We ended up going through every one of our forms and adding methods to detach all our events and we call these method in the Dispose event for the form. However it might be worth investigating a call to the Dispose method on the EventHandlerList [i.e this.Events.Dispose(); ]. Calling that might be enough to break the root references chain in your case.

Ian

Ian
Posts: 9
Joined: Thu Dec 08, 2005 10:14 am

Post by Ian » Wed Feb 01, 2006 12:15 pm

Guest wrote:...
oops, forgot to login. Didn't realise that Guest can post without any warnings you are about to post anon.

Ian

tenorcnj
Posts: 1
Joined: Fri Feb 10, 2006 2:53 pm

Post by tenorcnj » Fri Feb 10, 2006 2:56 pm

Ian wrote:
Guest wrote:...
oops, forgot to login. Didn't realise that Guest can post without any warnings you are about to post anon.

Ian
Hi Ian...

Can you give me just a bit more detail on how you unlink those events? I have a fairly large form which is getting left behind everytime I close it, and the memory profiler shows in the root path of the instance details:

Code: Select all

System.Windows.Forms	TabControl	#18,745
System.Windows.Forms	TabPage	#18,817
System.Windows.Forms	GroupBox	#18,829
Bdap.Library.BdapControls	BDAPUltraGrid	#18,805
System	EventHandler	#20,324
Infragistics.Win	XPThemes	ThemeChanged

Andreas Suurkuusk
Posts: 1029
Joined: Wed Mar 02, 2005 7:53 pm

Post by Andreas Suurkuusk » Mon Feb 13, 2006 7:53 am

The root path indicates that the static Infragistics.Win.XPThemess.ThemChanged event contains an event handler that references an instance of BDAPUltraGrid. This will prevent the BDAPUltraGrid instance from being garbage collected, and since all controls reference their parent, the parent Form and all child controls will also be prevented from being garbage collected.

You must make sure that the event handler that connects the BDAPUltraGrid instance with the ThemeChanged event is properly removed when the instance is disposed.
Best regards,

Andreas Suurkuusk
SciTech Software AB

Guest

Post by Guest » Mon Feb 13, 2006 1:24 pm

Andreas Suurkuusk wrote:The root path indicates that the static Infragistics.Win.XPThemess.ThemChanged event contains an event handler that references an instance of BDAPUltraGrid. This will prevent the BDAPUltraGrid instance from being garbage collected, and since all controls reference their parent, the parent Form and all child controls will also be prevented from being garbage collected.

You must make sure that the event handler that connects the BDAPUltraGrid instance with the ThemeChanged event is properly removed when the instance is disposed.
Yes, that's exactly as I thought. I just have to sort out how to do it. As far as I can tell, BDAPUltraGrid contains a control in the Infragistics.Win namespace... I don't see in the Windows Form Designer code anywhere that there is an event handler specifically linked. What should I look for?

Andreas Suurkuusk
Posts: 1029
Joined: Wed Mar 02, 2005 7:53 pm

Post by Andreas Suurkuusk » Mon Feb 13, 2006 2:48 pm

If the BDAPUltraGrid class is not part of "your" code, and you don't have access to the source, things get a little harder. You might be able to find out where the event handler is added by using Reflector (http://www.aisto.com/roeder/dotnet/). The event handler is probably added by some code in the BDAPUltraGrid class, possibly in the constructor. Once you have found out, you could also look if there is some code that removes the event handler. If there is, you can hopefully solve this problem by calling this code. Otherwise you can:
  • 1) Contact the creator of the class and ask them to correct the problem (recommended)
    2) Use reflection to remove the event handler.
    3) Remove the BDAPUltraGrid control from its parent. This will not remove the memory leak, but will reduce it substantially (only the control instance itself will be kept in memory, the rest of the form will be GCed).
Best regards,

Andreas Suurkuusk
SciTech Software AB

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 16 guests