Event handling - Lifetime management
#1 Members - Reputation: 246
Posted 18 November 2012 - 03:00 PM
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.
#2 Moderators - Reputation: 8420
Posted 18 November 2012 - 04:18 PM
[Work - ArenaNet] [Epoch Language] [Scribblings] [Journal - peek into my shattered mind]
#4 Members - Reputation: 481
Posted 18 November 2012 - 04:46 PM
#5 Moderators - Reputation: 8420
Posted 18 November 2012 - 04:47 PM
[Work - ArenaNet] [Epoch Language] [Scribblings] [Journal - peek into my shattered mind]
#6 Members - Reputation: 246
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>& spA)
{
std::cout << "B" << std::endl;
m_wpA = std::weak_ptr<A>(spA);
spA->m_kDelegate = RFMakeDelegate(this, &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 Members - Reputation: 3371
Posted 18 November 2012 - 05:10 PM
#8 Moderators - Reputation: 8420
Posted 18 November 2012 - 05:18 PM
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.
[Work - ArenaNet] [Epoch Language] [Scribblings] [Journal - peek into my shattered mind]
#9 Members - Reputation: 688
Posted 18 November 2012 - 08:24 PM
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 Members - Reputation: 481
Posted 19 November 2012 - 06:22 AM






