WPF - Memory leak in MS.Win32.HwndSubclass

Use this forum for questions on how to use .NET Memory Profiler and how to analyse memory usage.
Post Reply
gigios
Posts: 6
Joined: Fri Nov 16, 2012 8:59 am

WPF - Memory leak in MS.Win32.HwndSubclass

Post by gigios » Fri Jun 07, 2013 7:26 am

Hello, I'm investigating on some memory leaks in my WPF application with the last Memory Profiler.

In the snapshots of my application I see always this message:
Undisposed instances (release resource)
One type has instances that have been garbage collected without being properly disposed.
Investigate the type below for more information.

MS.Win32.HwndSubclass
After some investigation I found the cause but not the solution.
The problem seems related to the show of a new window.

To replicate the problem it is possible to make a small project with a MainWindow with a button, in the click event I have added this code:

Code: Select all

var win = new Window1();
win.ShowDialog();
win = null;
The second window (Window1) is completely empty.

1. Start the profiler of my application
2. Make a first snapshot without open the second window
3. Show the second window and after close it
4. Make a second snapshot and I see the "undisposed instance"

Also if I force the Garbage Collector, the object stay live.

There is a solution for this problem?

I don't have any idea on how avoid this, on internet I have found also some tricks, for example to use the API SendMessage to notify the close, but the result it is the same.

Regards.

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

Re: WPF - Memory leak in MS.Win32.HwndSubclass

Post by Andreas Suurkuusk » Mon Jun 10, 2013 3:09 pm

I have not been able to reproduce the issue. I have tested with .NET Framework 3.0, 4.0 and 4.5. What framework version are you using?

Are you actually seeing a memory leak of HwndSubclass (i.e. do you see any new live instances)? An undisposed instance has usually been garbage collected and is normally just a performance issue. If the instance is properly disposed, any unmanaged resources will be released quicker, and a costly finalization can be suppressed.
Best regards,

Andreas Suurkuusk
SciTech Software AB

gigios
Posts: 6
Joined: Fri Nov 16, 2012 8:59 am

Re: WPF - Memory leak in MS.Win32.HwndSubclass

Post by gigios » Wed Jun 12, 2013 6:15 pm

Unfortunately I can use only the framework 3.5 for the current application (but I have seen the same behavior also with the framework 4.0).

On the small project I have 5 live instances, but stay alive also after a long time and also after calling the GC.Collect.

On my real application the live instances are 7 or more.

I have attached the small project used for the test.

Thanks for the answer.
Attachments
Test.zip
Small Test Project
(9.48 KiB) Downloaded 446 times

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

Re: WPF - Memory leak in MS.Win32.HwndSubclass

Post by Andreas Suurkuusk » Thu Jun 13, 2013 8:28 pm

Yes, in your test project you have 5 live instances of HwndSubclass, but that doesn't indicate a memory problem. These instances are used by WPF for subclassing HWNDs and they are kept alive by the _hwndList hashtable in ManagedWndProcTracker.
hwndsubclassgraph.png
HwndSubclass graph
When you open and close a window an undisposed HwndSubclass will be created, but as I mentioned previously, this doesn't cause a memory leak since the instance has been GCed (there's no new live instances).

The allocation call stack of the undisposed instance shows that it was created by the ShowDialog method.
hwndsubclassundisposed.png
Undisposed allocation stack
By using ILSpy, you can see that the HwndSubclass is not disposed under some circumstances (to avoid "unsubclassing" the HWND when the window is being destroyed). In conclusion, the undisposed HwndSubclass can be ignored in your case. We will probably add this as a default ignored issue, or as an ignored framework issue, in a future version of the profiler.
Best regards,

Andreas Suurkuusk
SciTech Software AB

gigios
Posts: 6
Joined: Fri Nov 16, 2012 8:59 am

Re: WPF - Memory leak in MS.Win32.HwndSubclass

Post by gigios » Fri Jun 14, 2013 8:27 am

Thanks for the answer :)

I'm still learning ".NET Memory Profiler" and it is not always easy to understand what may be a memory leak and what not :oops:

Thank you again

abhawasinka
Posts: 2
Joined: Fri Jul 19, 2013 4:08 am

Re: WPF - Memory leak in MS.Win32.HwndSubclass

Post by abhawasinka » Fri Jul 19, 2013 4:18 am

The issues mentioned above/below is leak caused due to the user and is NOT a bug in WPF.

NOTE: The below explanation is for WPF but is very similar to WinForms.

When the first UI object (WPF) is created on a thread, a Dispatcher object is created which creates a handle. If you are not creating/showing the window using the Application class provided by the WPF application, then you must shutdown the Dispatcher (InvokeShutdown) else all the handles associated with the Dispatcher will not be freed even when the thread has exited.

Code: Select all


Dispatcher dispatcher = Dispatcher.CurrentDispatcher;
...
//Create and display your UI
...
dispatcher.InvokeShutdown();
-
Regards,
Ashutosh Bhawasinka

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 29 guests