Replies: 1 comment
-
General answer... When an object implements IDisposable it should remove all possible event handlers that reference it. Remember Dispose is for a VERY specific purpose. The entire Weak Reference paradigm is designed for a completely different use case, allowing an objeect to be Garbage collected as there are no more strong refences to it. An object can be Disposed while still having Strong References to It. And (depending on user implementation) an object can be Garbage Collected without Dispose ever being invoked. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi!
I'm using WeakReferenceMessenger in my WinUI3 app.
I have a UserControl-derived class that is registers itself as the recipient of a message.
I have this UserControl on a Page, working happily, and then I navigate away from that page to a different page.
Then, some short time later, the WeakReferenceMessenger is asked to send a message, to which the former UserControl instance was registered as a recipient.
Sometimes (not all the time), this UserControl instance still receives the message, and the message handler method throws with an ObjectDisposedException (that I cannot catch).
I was under the impression that WeakReferenceMessager would safely handle this condition and stop notifying disposed objects?
(Note that I did not explicitly dispose of my UserControl instance. I only navigated away from the Page who had an instance of the UserControl as an element.)
Also, perhaps relevant, the message handler method in the UserControl does access a DependencyProperty on the UserControl. That is where the exception is first thrown: in the implementation of DependencyObject.GetValue().
Is this the intended behavior? And if so, what can be done to be able to have a UserControl as a message recipient? This is a fairly important part of the design of my application - user controls update their state in response to 'global' changes in the application state that are sent through the WeakReferenceMessenger.
I am using version 8.2.2 of CommunityToolkit.MVVM.
Thanks for any guidance you can provide,
-Eric.
Beta Was this translation helpful? Give feedback.
All reactions