C# ContextMenuScript cause GCHandle leak and Memory Leak in .Net Framework

46 views Asked by At

I developed an application in .net framework 4.8, it can show, switch and close different .net displays via commands with many controls inside of the display again and again, I cannot hold them in memory, because it's too many (depend on user requirement, manybe > 5,000 displays, 1000,000 .net controls), so I must use "new" and "Dispose". After switching for a while, we find the memory leak, especially GCHandle leak in Performance Monitor.

enter image description here

Debug with WinDBG, find System.WeakReference is leak, stop the switching for a long time, then print the handle value of WeakReference in WinDBG, all are null. Notice: I already called the dispose, like b.ContextMenuScript = c; I already called:

b.ContextMenuScript = null;
... // clear all event bind what I binded, clear all children in menu
c.Parent = null;
c.Dispose();
c = null;

What can I do right now to fix this anomaly? Or does Microsoft give some document to describe it, do something, or it's unknown issue?

I already remote the ContextMenuScript from parent, and call it's Dispose method, it still leak. I know it maybe hold in a static object in system, but how to remove it, and how to reduce the GCHandle?

0

There are 0 answers