It’s a common pattern that one has a class subclassing
x.addListener (this) in its own constructor and
x.removeListener (this) in its destructor.
It would be less error-prone if the destructor of
X::Listener would remove the listener on its own. This would require the listener to keep a list of what objects it listens to.
An alternative would be to maintain this list only in Debug builds, with an assertion in the destructor that the required
removeListener calls have been made. This would enabled catching a class of possible bugs earlier.