Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear memoryleak from ElementHost Control

I have a requirement to host WPF Control on Winform User control. To achieve this I used ElementHost control. When I run Ants Memory profiler , I got know that There is a Huge memory leak in the ElementHost control. Please find the attached retention graph as below and kindly help me to fix the Memoryleak .enter image description here

like image 206
naveen Avatar asked Oct 31 '22 20:10

naveen


1 Answers

I ran into exactly same memory leaking situation with exactly same symptoms. Here's how I handled the situation.

ElementHost class have PropertyMap property which is collection that maps WinForms control property to WPF control property.

In this particular case, memory is leaking through BackgroundImage that keeps MemoryStream instance. And so, the solution is to remove BackgroundImage property mapping:

elementHost.PropertyMap.Remove("BackgroundImage");
like image 182
Vadim Avatar answered Nov 15 '22 04:11

Vadim