Maybe… but what I’m advocating here is a style that doesn’t require you to have to think about it. The ListenerList paradigm is becoming more and more antiquated with the pervasiveness of
These suffer from the same problem but really, how often is it that you’re listening to an object that isn’t a member? Most Buttons, Sliders, TextEditors etc. you have local listeners or
onClick callbacks so you don’t need to unregister them.
I’d argue that if you’re listening to a lot of objects from outside your Components then there is probably a better model to be used such as a ValueTree that you can take a local copy of.
The only real places we have to remove listeners is when you have global type objects (like MIDI learn) or more legacy classes. It’s a relatively rare problem.
Static analysis can probably help identify these problems and Asan certainly if you can catch it in the act in a debugger.
Also making ListenerList store WeakReferences is a bit tricky because then all of your classes would have to store a master reference which can bloat a lot of simple classes.
In my experience WeakReferences are most useful for async code and threading. In most other situations it’s better to handle the lifetimes manually or you spend half your time purging dead weak refs. (But I could be swayed on this, other languages have weak-refs built in which can lead to nice non-owning/observing semantics).