Possible memory leak in WinUI 3 Preview 3 when using ContentDialogs and UserControls

vadd98 1 Reputation point
2021-01-22T16:51:42.6+00:00

I'm developing a Win32 Net5 app using the WinUI 3 Preview 3 and I'm getting some problems with possible memory leaks. I have a complex app which uses a lot of UserControl and ContentDialogs but, after they are out of scope, their memory is never released.

I'm trying to do some memory analyses using DotMemory and in the screenshot below it is possible to see one of the UserControl (named SongControl) that is never released. In particular, it is possible to see that the only thing that is retaining the UserControl is the so called "Regular Handle", which I think is a GCHandle from the internal WinUI code.

This SongControl is used in a ListView and is generated using an ObservableCollection as ItemSource. When I navigate away from the page with the ListView, I remove all the items present in the ObservableCollection (removing also all the event handlers linked to each item and setting all of them to null) and then set the collection to null.

As there is nothing in particular retaining the control, I believe that in a standard situation the internal WinUI code would have to remove that Regular Handle and allow the object to be collected, but it doesn't happen. Because of this, the memory footprint of my app keeps growing forever and so, soon or later, the user will get an OutOfMemoryException.

The same behaviour happens for every ContentDialogs I use. When they are closed, they are never released from memory, even setting them to null and removing all event handlers to which they are linked.

I already tried to set each UserControl and each ContentDialogs I use to null, set each control they contain to null, remove all event handlers, but nothing changed.

Any ideas on how to solve the problem?

59626-immagine-2021-01-21-230238.png

Universal Windows Platform (UWP)
{count} votes