0

Reading about the Weak Event Patterns it is clear to me that it allows for garbage collecting the listener when it is not longer referenced but is the reciprocal true also ? Meaning does this pattern allows for garbage collecting the source when it is no longer referenced (except via attached weak listener) ?

Why am I asking this: I want to implement a CopyOnWrite pattern between some "source" and "listener" where the second one is just a subset of the first one. So I need to "detach" whenever one write to the source/listener or when the source disappears (because no need to keep a large amount of data while only a subset is still referenced). So I need very loose coupling between the source and the listener (i.e. letting them both be garbage collected whenever one or the other is no longer strongly referenced).

CitizenInsane
  • 4,755
  • 1
  • 25
  • 56
  • 1
    This pattern solves a problem where a source keeps a listener object alive longer that it's natural lifetime. Note that *the opposite problem doesn't occur*, the source can always be collected whether or not it still has listeners, if it is otherwise no longer reachable. – Damien_The_Unbeliever Nov 29 '21 at 11:07
  • Ok thanks @Damien_The_Unbeliever ... Reading documentation confused me, yes source event is classic `Event` as usual, so no need to worry in fact. This question can be closed :) – CitizenInsane Nov 29 '21 at 17:58

0 Answers0