WPF PRISM event subcriptions not dying
Asked Answered
A

1

2

I have a PRISM RegionManager with a couple of region - a Ribbon region on the top, and a main content region for my view underneath it - fairly basic.

The app starts with a "home" view in the main content area. When I click a button on the Ribbon, I inject a second view into the content area and navigate to it in the region manager. When I click a button on this view, it should be removed and the original view should be shown.

I'm currently doing this using the RegionManager.Add() method to manually add the second view. When I want to remove it, I publish an event which is consumed by a manager class that gets the current active remove, calls Remove() on the Region Manager for it, and then navigates back to the original view.

This all works great, except that when the second view is left alive after I call Remove(), and keeps a handle onto any subscriptions that it made during its lifetime! I've tried calling Subscribe explicitly with false for weak event references, but this is the default anyway, and it didn't help. I've tried both types of creating the view (discovery and injection) and removing the view via Remove and Deactivate. None of them helped. This is a real problem as when I want to create the same view in the future (a new instance of that view), I'm left with several instances of them, all subscribing to the same events, even though some of those instances were removed ages ago from the Region Manager.

Do I need to manually unsubscribe from every event that I subscribe to in my views (highly undesirable)? Or is there some way that I can dispose of the control / mark it as deactivated so that its subscriptions get removed?

I get the feeling that I'm doing something silly here but it's got me completely stumped.

Thanks

Audile answered 30/11, 2010 at 4:58 Comment(0)
R
1

This is a known issue. There is a bug in the eventAggregator code. Take a look at the following blog and the suggested hack to overcome it https://greenicicle.wordpress.com/2010/04/28/prism-event-aggregator-more-leaky-than-it-seems/

Hope this helps

Rackety answered 30/11, 2010 at 9:19 Comment(4)
Thanks for the heads up - just read through that post and have found that bug and used the suggested workaround - whilst this does force PRISM to use a weak reference, unfortunately I'm still getting the same behaviour :-(Audile
I've looked at it a bit more. I think that this sort of problem is more fundamental - if you wait around long enough, the weak references DO get cleared from the event subscription service. But if they haven't been cleared - and you can never know when that will be - then the old subscriptions can be left lurking around, listening to all sorts of events. I think either I'll have to go down the route of manually unsubscribing from any event whenever I close a view down, or reusing single instances of a view rather than creating new instances. Neither appeal to me much though.Audile
@Isaac Abraham: this is because when a weak reference is maintained, it is still valid until the garbage collector comes along and cleans up that object. If you want immediate unsubscription, you'll have to manually unsubscribe, unfortunately.Brunelle
Yep, that's the conclusion that I came to. Cheers.Audile

© 2022 - 2024 — McMap. All rights reserved.