Jason Winnebeck wrote: [...]
I'm having a problem with trying to mitigate reference cycles in a event generator/event listener paradigm. One creates a listener object, and registers itself as a listener of that object. As a result of the event, it may want to invoke methods of that object, so it contains a reference to that object.
So Generator contains a reference to Listener, and Listener contains a reference to Generator. This is a problem with reference counted pointers.
There are no inherent cycles so far in the design. A typical Listener interface is struct Listener { virtual void accept(Event const & event, Generator & sender) = 0; }; so that it can listen to several Generators at a time. The assumption is that Listener::accept cannot destroy 'sender'. Logically, if a Generator does not own its Listeners, it should keep weak pointers. The advantage is that dead Listeners that haven't unregistered can be auto-unsubscribed by the Generator (unless you have real measurements that indicate a performance problem with weak_ptr::lock(), which would surprise me :-).) HTH