Jump to content

  • Log In with Google      Sign In   
  • Create Account

Event handling - Lifetime management


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
9 replies to this topic

#1 elurahu   Members   -  Reputation: 254

Like
0Likes
Like

Posted 18 November 2012 - 03:00 PM

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.

Sponsor:

#2 ApochPiQ   Moderators   -  Reputation: 15815

Like
0Likes
Like

Posted 18 November 2012 - 04:18 PM

What language are you using?

#3 elurahu   Members   -  Reputation: 254

Like
0Likes
Like

Posted 18 November 2012 - 04:24 PM

C++

#4 _swx_   Members   -  Reputation: 944

Like
0Likes
Like

Posted 18 November 2012 - 04:46 PM

http://code.google.com/p/cpp-events/ has a signals&slots implementation with connection tracking.

#5 ApochPiQ   Moderators   -  Reputation: 15815

Like
0Likes
Like

Posted 18 November 2012 - 04:47 PM

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.

#6 elurahu   Members   -  Reputation: 254

Like
0Likes
Like

Posted 18 November 2012 - 04:50 PM

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.

Edited by elurahu, 18 November 2012 - 04:50 PM.


#7 Telastyn   Crossbones+   -  Reputation: 3726

Like
1Likes
Like

Posted 18 November 2012 - 05:10 PM

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).

#8 ApochPiQ   Moderators   -  Reputation: 15815

Like
0Likes
Like

Posted 18 November 2012 - 05:18 PM

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.

#9 wqking   Members   -  Reputation: 756

Like
1Likes
Like

Posted 18 November 2012 - 08:24 PM

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

http://www.cpgf.org/
cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.
v1.5.5 was released. Now supports tween and timeline for ease animation.


#10 _swx_   Members   -  Reputation: 944

Like
0Likes
Like

Posted 19 November 2012 - 06:22 AM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS