The following test suggests that, no, the scenario does not result in a memory leak.
public partial class LeakTest : UserControl
{
public ICommand PopupCommand { get; private set; }
public LeakTest()
{
InitializeComponent();
PopupCommand = new DelegateCommand(arg =>
{
var child = new ChildWindow();
child.Closed += (sender, args) =>
{
System.Diagnostics.Debug.WriteLine("Closed window");
};
// when the window has loaded, close it and re-trigger the command
child.Loaded += (sender, args) =>
{
child.Close();
PopupCommand.Execute(null);
};
child.Show();
});
}
}
The reason is suggested in the answer to the (Winforms) post linked to by Jwosty:
In your example, the publisher only exists within the scope of your private method, so both the dialog and the handler will be garbage collected at some point after the method returns.
In other words, the memory-leak concern is really the other way around -- the event publisher (the ChildWindow
control) holds a reference to the subscriber (the DelegateCommand
), but not the other way around. So, once the ChildWindow
is closed, the garbage collector will free its memory.