Event handling - Lifetime management

Started by
8 comments, last by _swx_ 11 years, 5 months ago
Been looking into introducing delegates in my code base and I've run into some lifetime problems I was hoping to get solved.
Currently I'm using fastdelegates for my dispatching.

My problem is the following:

- Object A can raise an event (It contains a delegate)
- Object B registers a method to the delegate in A.
- Object A raises an event and method gets called in B.

Now this can go wrong in two ways:

- Object B is killed and A tried to raise the event. Crash. This is easily solved by making B unregister from A when it dies.
- Object A is killed and B is killed. When B is killed it tries to unregister from A which is no longer there!

The second is giving me problems and I was hoping someone could point me to a proper solution.
Advertisement
What language are you using?

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

C++
http://code.google.com/p/cpp-events/ has a signals&slots implementation with connection tracking.
The canonical solution in C++ is to use a smart pointer to keep track of the lifetime of your objects. Be warned that this might get sticky if you need circular references. I recommend doing a thorough bit of research into shared_ptr and weak_ptr.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]


struct A
{
RFDelegate1<int> m_kDelegate;
A()
{
std::cout << "A" << std::endl;
}
~A()
{
std::cout << "~A" << std::endl;
}
void DoWork()
{
if (!m_kDelegate.empty())
m_kDelegate(69);
}
};
struct B
{
std::weak_ptr<A> m_wpA;
int m_i;
B(const std::shared_ptr<A>&amp; spA)
{
std::cout << "B" << std::endl;
m_wpA = std::weak_ptr<A>(spA);
spA->m_kDelegate = RFMakeDelegate(this, &amp;B::Handler);
}
~B()
{
std::cout << "~B" << std::endl;
if (!m_wpA.expired())
{
m_wpA.lock()->m_kDelegate.clear();
}
std::cout << "Deleted" << std::endl;
}
void Handler(int i)
{
m_i = i;
std::cout << "Handled " << m_i << std::endl;
}
};

// Usage
{
std::shared_ptr<A> spA = std::make_shared<A>();
B* pkB = new B(spA);
spA.reset();
delete pkB;
}


Well this is what I was doing now - But it's hardly a "pretty" solution. But I guess that's just C++ for you.
At least in my neck of the woods (C# business apps), events/delegates have fallen largely out of favor for this sort of scenario, precisely for these reasons. They're only used now for scenarios where the event and it's handler are guaranteed to have relative lifetimes (a widget and it's child, two members of a screen mediated by the screen view itself, etc.). This often means creating that sort of mediator that can wire up the event properly and knows enough to unwise it when the time comes (or using a different decoupling approach).
Telastyn raises a great point.


A much better architecture is to have a dispatcher that contains a list of all objects and all events that need to be shuffled around. The dispatcher runs periodically (say, once a frame) and fires off every event that still makes sense, i.e. is linked to still-valid objects. Then the problem just becomes ensuring things occur in the right order, which is generally not too hard.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

OP, what you need is called "connect tracking", which is available in Boost::signal and libsigc++.
Fastdelegate is not a comlete signal/slot system.

https://www.kbasm.com -- My personal website

https://github.com/wqking/eventpp  eventpp -- C++ library for event dispatcher and callback list

https://github.com/cpgf/cpgf  cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.

The library in the url I provided uses the FastDelegates library and implements connection tracking (using scope objects), which is exactly what the OP is asking for. I would however suggest that you try to avoid these types of event systems since keeping track of the flow of execution easily becomes almost impossible.

This topic is closed to new replies.

Advertisement