Do I need to unsubscribe from events when closing a window in wpf?

tiltle says everything, in my window (and not in the main window) I have a constructor

EventAggregator.OnUserLoggedIn += OnUserLoggedIn;
EventAggregator.OnUserLoggedOff += OnUserLoggedOff;

is there a difference between

this.Close()

and

EventAggregator.OnUserLoggedIn -= OnUserLoggedIn;
EventAggregator.OnUserLoggedOff -= OnUserLoggedOff;
this.Close()

I read that closing a window provides all unused resources, are events that are considered maneuverable or uncontrollable?

+4
source share
2 answers

These events are resource driven if anything (I'm not sure if you can call event resources, though).

Yes, you must disable event handlers from child windows (not the main window, since it does not matter), otherwise you may encounter memory leaks, as the garbage collector will not be able to pick up these objects that still have links.

:

, .NET, . , - , , , , ​​ .

+5

closing the main window, , don't have to worry about unhooking the handler. , , .

talking about secondary window , consider unhooking the events, , EventAggregator . Publisher (EventAggregator) will hold on to the subscriber object (window) as long as it stays in memory. , EventAggregator.

+4

All Articles