Evaluating .NET Memory Profiler - need some assistance

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

Moderator: SciTech Software

Evaluating .NET Memory Profiler - need some assistance

Postby jgodfrey » Thu Aug 29, 2013 7:10 pm

I'm currently working on a WPF-based application that allows a User to open multiple instances of a WinForms graphical control in a TabControl. The WinForms control is wrapped in a WinFormsHost-based UserControl.

For some reason, instances of the UserControl are not being released when their associated tab is closed. I've spent the better part of a day using a demo copy of .NET Memory Profiler trying to determine the root cause. From what I can tell, the problem seems to revolve around the WinFormsAdapter that contains my User Control, but I can't quite see my way through the trees.

If anyone could take a look at my saved session and give me a push in the right direction, that'd be great.

To create the session, I:

- Started my app
- Opened a single copy of my User Control in a tab
- Closed the tab
- Took snapshot #1
- Opened a new instance of my User Control
- Closed the tab
- Took snapshot #2

Comparing the snapshots, I can see that there are now 2 instances of my User control in memory. This scenario can be repeated to create additional (leaked) copies of the control.

In the referenced profiler session, the control is in the "CustomControls" namespace and is named "ViewportCustomUC".

The snapshot is here: https://www.dropbox.com/s/xz554acush2ba ... prfsession

Thanks for any assistance you can provide.

Jeff
jgodfrey
 
Posts: 2
Joined: Thu Aug 29, 2013 6:49 pm

Re: Evaluating .NET Memory Profiler - need some assistance

Postby Andreas Suurkuusk » Fri Aug 30, 2013 2:25 pm

If you investigate the new instance of WindowsFormsHost in your second snapshot, you will see that the instance is indirectly held by a ProcessInputEventHandler. The allocation call stack of the event handler reveals that it was created in the WindowsFormsHost constructor (via the constructor of the internal WinFormsAdapter class).
WindowsFormsHost.png
ProcessInputEventHandler holding WindowsFormsHost

Using ILSpy, you can see that WinFormsAdapter adds an event handler to InputManager.PostProcessInput, and this event handler is only removed in the Dispose method of WinFormsAdapter. So to make sure that the event is properly released, the WinFormsAdapter must be disposed (by calling WindowsFormsHost.Dispose) when you are done with your control.

I Googled "WindowsFormsHost dispose" and found an article at http://blogs.msdn.com/b/scoberry/archiv ... 35844.aspx. It contained the following information:

When mixing Windows Forms and WPF to make sure the ElementHost or WindowsFormsHost is disposed, or you could leak resources. Windows Forms will dispose an ElementHost when the non-modal Form it’s on closes; WPF will dispose a WindowsFormsHost if your application shuts down. (Really the interop-specific bit here is that you could show a WindowsFormsHost on a Window in a Windows Forms message loop and never get that your Application is shutting down.)


I hope this helps.
Best regards,

Andreas Suurkuusk
SciTech Software AB
Andreas Suurkuusk
 
Posts: 959
Joined: Wed Mar 02, 2005 7:53 pm
Location: Sweden

Re: Evaluating .NET Memory Profiler - need some assistance

Postby jgodfrey » Fri Aug 30, 2013 4:17 pm

Andreas,

Thanks for your informative reply. Looking at my code, when my WindowsFormsHost-based User Control was unloaded, I had specifically called Dispose() on the underlying WinForms control, but not on the WindowsFormsHost itself. Adding an additional dispose call to kill the host itself seems to have fixed the leak. Awesome!

Again, thanks so much for your input.

Jeff
jgodfrey
 
Posts: 2
Joined: Thu Aug 29, 2013 6:49 pm

Re: Evaluating .NET Memory Profiler - need some assistance

Postby ivan_83 » Thu Feb 26, 2015 8:14 am

Thanks for this question and answer, I also ran into the similar situation (I used ElementHost) but the problem and resolution were the same. .NET Memory Profiler rocks!
ivan_83
 
Posts: 1
Joined: Thu Feb 26, 2015 8:12 am


Return to Using .NET Memory Profiler

Who is online

Users browsing this forum: Bing [Bot] and 1 guest

SciTech Software logo

© Copyright 2001-2016. SciTech Software AB
All rights reserved.


SciTech Software AB
Kartvägen 21
SE-175 46 Järfälla
Sweden


E-mail: mail@scitech.se

Telephone: +46-706868081

cron