mahapps.com icon indicating copy to clipboard operation
mahapps.com copied to clipboard

DialogParticipation.Register="{Binding}" Memory leak

Open svantevit opened this issue 4 years ago • 2 comments

I am using MahApps.Metro, v1.6.5.

MetroWindow with mah:DialogParticipation.Register="{Binding}" doesn't get disposed after the ShowDialog() call.

Example usage: I have two windows - MainWindow and MyDialogView. Each has it's own view model - MainWindowViewModel and MyDialogViewModel. MainWindowViewModel contains a OpenDialogCommand, which opens the dialog view. MyDialogViewModel contains a CloseCommand, which raises an OnClose event. The MainWindowViewModel handles the OnClose event and closes the dialog window.

For easier debugging I instantiate a large byte array in the MyDialogViewModel constructor. If I open and close the dialog window multiple times the memory usage only increases (by 1GB). But if I remove the DialogParticipation.Register parameter, then the memory usage gets decreased when the garbage collector runs.

I prepared an example here

svantevit avatar Nov 05 '19 08:11 svantevit

@kmetic You're correct with this memory leak. I must apologize that the hint for this is not easier to find. I'll put this on a better place.

https://github.com/MahApps/MahApps.Metro/blob/94fee344eb91f977a7289d5c8c7b89861605a198/src/MahApps.Metro.Samples/MahApps.Metro.Demo/MainWindow.xaml#L26-L32

punker76 avatar Nov 05 '19 10:11 punker76

@punker76 thanks, I tested it and it works.

One question - I tried to unregister even if I didn't register before and it looks OK. Is it wrong to unregister all MetroWindows by default, event I you don't use DialogParticipation at all?

svantevit avatar Nov 06 '19 09:11 svantevit