jeffbeat
jeffbeat

Reputation: 69

WPF memory leak with custom control

I want to dispose of a control I'm dynamically adding to my app. The garbage collector is not picking up the object after I .Remove() it from its parent control, and it has huge bitmaps and geometry private members.

I want to be able to do something like this:

foreach (ScrollItem mylabel in canvas1.Children)
{
    if (mylabel.bRemove == true)
    {
        canvas1.Children.Remove(mylabel);
        mylabel = null;  // or mylabel.Dispose();
    }
}

canvas1 can't have null items in a UIObjectCollection so I can't set it to null, and if I just Remove() it the garbage collector doesn't collect it.

I tried to do something like:

myobj = mylabel;
canvas1.Children.Remove(mylabel);
myobj = null;

but that doesn't work either.

Upvotes: 1

Views: 852

Answers (1)

sam1589914
sam1589914

Reputation: 816

As long as there is no references to your object the GC will collect it. Check your app for any additional references to your object. This can typically be event subscribers. You might want to use a .NET memory profiler too see what is preventing your object from beeing GC'ed.

Upvotes: 4

Related Questions