Reputation: 2168
I'm currently working on an application where I am using WinForms WebBrowser controls in WinForms hosts. The problem is even when I close a browser and dispose of everything the memory doesn't seem to be released.
I found this post and it looks like udione's answer might be what i'm looking for. The problem is I'm not sure how to get it to work with the WinForms control through WPF.
How to get around the memory leak in the .NET Webbrowser control?
Here is what I changed it too but it doesn't seem to do the trick. I'm not sure about the this.webbrowser.webBrowser
public void DisposeWebBrowser(System.Windows.Forms.WebBrowser browser)
{
//dispose to clear most of the references
browser.Dispose();
//REMOVED THIS LINE
//BindingOperations.ClearAllBindings(browser.);
//using reflection to remove one reference that was not removed with the dispose
var field = typeof(System.Windows.Window).GetField("_swh", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
//CHANGED THIS LINE
//var valueSwh = field.GetValue(mainwindow); <-- OLD LINE
var valueSwh = field.GetValue(GetWindow(this));
var valueSourceWindow = valueSwh.GetType().GetField("_sourceWindow", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).GetValue(valueSwh);
var valuekeyboardInput = valueSourceWindow.GetType().GetField("_keyboardInputSinkChildren", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).GetValue(valueSourceWindow);
System.Collections.IList ilist = valuekeyboardInput as System.Collections.IList;
lock (ilist)
{
for (int i = ilist.Count - 1; i >= 0; i--)
{
var entry = ilist[i];
var sinkObject = entry.GetType().GetField("_sink", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
//CHANGED THIS LINE
//if (object.ReferenceEquals(sinkObject.GetValue(entry), this.webbrowser.webBrowser)) <-- OLD LINE
if (object.ReferenceEquals(sinkObject.GetValue(entry), browser))
{
ilist.Remove(entry);
}
}
}
}
Any ideas on how I can get this to work or a better way to handle the problem?
Upvotes: 2
Views: 528
Reputation: 26268
Yes. Any WebBrowser that .NET has and involves IE, is pure evil and should be never used, since its behavior varies from computer to computer. Use either CefSharp(prefered) or Awesomium.
Upvotes: 2