Closed DaveInCaz closed 2 years ago
Hi @DaveInCaz,
Yes, you can just recreate a new instance. It might make sense to add a Clear
method, though!
Thanks. The reason I wondered about clearing it is if that would have some advantage in freeing the subscribers sooner, like if it would allow an earlier garbage collection than if the weak event source were itself just dereferenced. If not (which is what it sounds like), then it probably isn't needed.
Admittedly this is like the opposite problem that weak events were intended to solve :)
The reason I wondered about clearing it is if that would have some advantage in freeing the subscribers sooner
Why would you want to free the subscribers sooner, though? They decided to subscribe to the event, so if they're still "alive" and don't explicitly unsubscribe themselves, why would you forcibly unsubscribe them? I think it's a pretty rare situation where you know better than the subscriber when it should be unsubscribed...
like if it would allow an earlier garbage collection than if the weak event source were itself just dereferenced
Being subscribed to the WeakEventSource doesn't prevent them from being garbage collected (that's the whole point, after all :wink:)
I'm not saying there can't be any reason to clear all subscribers, but I don't think freeing them sooner is one of them.
I did have a situation where an event publisher was being removed (replaced, actually) but the subscribers were still alive, so a manual cleanup of those subscriptions seemed necessary. But I agree, this might not be too common of a scenario.
Sorry, my second comment about GC wasn't explained sufficiently - what I meant was whether clearing the subscribers would allow the weak event source itself to be GC'ed more efficiently than if you just dereference it. Sounds like it should not make any difference, but I'm not familiar enough with how the GC deals with the collection of weak references (handles?) that the source would contain to know for sure.
I did have a situation where an event publisher was being removed (replaced, actually) but the subscribers were still alive, so a manual cleanup of those subscriptions seemed necessary.
But in that case, the subscribers will need to be notified anyway, so that they can subscribe to the new publisher. So they can also unsubscribe themselves from the old publisher. That being said, it's true that it would be a good use case for a WeakEventSource.ClearSubscriptions()
method.
what I meant was whether clearing the subscribers would allow the weak event source itself to be GC'ed more efficiently than if you just dereference it
That won't make a difference. The publisher (WeakEventSource, or the object that holds it) references the subscribers, not the other way around. And even if the subscribers happen to reference the publisher, if they only reference each other, they're not reachable, so they can be GC'ed.
Closing due to inactivity
Thanks for creating this library, I'm finding it very useful.
With a standard C# event handler you could clear all subscribers by setting it equal to
null
from within its class. UsingWeakEventSource
, is there a way to do something equivalent?Maybe just creating a new
WeakEventSource<>
object is the way to do that?