Sign in to follow this  
elurahu

Event handling - Lifetime management

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[CODE]
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;
}
[/CODE]

Well this is what I was doing now - But it's hardly a "pretty" solution. But I guess that's just C++ for you. Edited by elurahu

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this