I've got a WPF TabControl containing a WindowsFormsHost in each TabPage. User can add and remove TabPage as needed: when the user removes a Tab, I obviously dispose the child control and the host itself.
Using VS Diagnostic Tool, I've found a leak of WindowsFormsHost:
I've also reproduced the issue using an empty WindowsFormsHost, with no inner child, tested with framework 4.0 and 4.7.2. Something like:
<Grid>
<WindowsFormsHost/>
How can I solve? GC.Collect() does not do the trick.
Solved removing the WindowsFormsHost element from parent layout:
public class WindowsFormsHostEx : WindowsFormsHost
{
public WindowsFormsHostEx() { }
protected override void Dispose(bool disposing)
{
if (this.Child != null && this.Child is IDisposable)
(this.Child as IDisposable).Dispose();
this.Child = null;
//magic line!!!
(this.Parent as Panel).Children.Remove(this);
base.Dispose(disposing);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.