Find control causing memory leak

Use this forum to discuss subjects that don't belong in the other forums.
Post Reply
Posts: 2
Joined: Tue Feb 16, 2010 8:48 pm

Find control causing memory leak

Post by tanderson66 » Tue Feb 16, 2010 9:02 pm

I am trying to find the memory leak(s) in one of our apps. When I take a snapshot (after opening/closing a child form) I see that there is an instance with a Direct EventHandler root. I click on the instance # to see the Type instance details but I can't find the name of the control causing the issue. I look on the Reference by, References and field value tabs and don't see it. Is there somewhere else I need to be looking?

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

Re: Find control causing memory leak

Post by Andreas Suurkuusk » Tue Aug 31, 2010 10:03 pm

If you investigate the root paths of the instance that has direct event handler roots, you should often be able to identify the instance (e.g. a Control instance) that is causing the issue. In the screenshot below, Form1 instance #49,617 owns the EventHandler that is causing the issue.
Form with EventHandler
EventHandlerForm.png (12.16 KiB) Viewed 32115 times
The field name that is used for the EventHandler can often provide a clue about the actual event that is keeping the reference. In the screenshot below, the idleHandler field is used to store a reference to an EventHandler that has been added to the Application.Idle event.
Idle event handler
IdleEventHandler.png (14.68 KiB) Viewed 32115 times
To minimize memory overhead, Windows Forms controls often use an EventHandlerList to store event handlers. This makes it a bit harder to identify the actual event. The allocation call stack of the EventHandler at the top of the root path (#22476 in the screenshot below) will provide information about why the event handler was added. This information is often all you need to identify the event, but to get additional information, the ListEntry can also be investigated.
Form event handlers
FormEventHandlers.png (13.21 KiB) Viewed 32115 times
The screenshot below shows the field values of ListEntry #22,480. The key field can be used to identify the event.
Event handler key
EventHandlerKey.png (8.58 KiB) Viewed 32115 times
Double clicking the key reveals the following root path for the key:
Static key field
StaticEventKey.png (8.57 KiB) Viewed 32115 times
From this you can draw the conclusion that the event handler was added to the Form.Activated event.
Best regards,

Andreas Suurkuusk
SciTech Software AB

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests