Search code examples
c#memorymemory-leakscomponentone

Memory Leak / Form not being garbage collected


I'm tracking down a Memory leak within an MDI application. Opening, and then closing the form results in the form staying in memory. Using Ant's memory profiler, I can get the following graph of references keeping the form in memory.

I have removed any and all events we attach to the combo controls when Dispose fires on the form.

Can anyone direct me towards a solution?

The C1 namespace comes from ComponentOne.

I should note that I have attempted to see what the c, r, b etc methods on the C1Combo control are via reflector, but its obviously been run through an obfusticator which makes things difficult to understand.

Ant's Reference Graph


Solution

  • Tracked it down to an internal reference to controls in the C1Combo. There is a list that for some reason has references to the forms and a few other things. In Dispose() on the form, I call this function against each C1Combo control. No idea on the consequences, likely minimal as the control should be Disposed anyway.

    It's also very fragile, as if they release a new version, and the obfustication messes up all the method/field names, it will break.

    private void RemoveInternalC1ComboReferenceListHack(C1Combo combo)
        {
            var result = typeof(C1Combo).GetField("_dropDownList", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(combo);
            var result2 = result.GetType().GetField("c", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(result);
            var result3 = result2.GetType().GetField("r", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(result2);
            var result4 = result3.GetType().GetField("b", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(result3);
    
            ((System.Collections.Generic.List<Control>)result4).Clear();
        }