typedef std::shared_ptr<EventListener> EventListenerPtr;BounceComponent::BounceComponent(ActorId actorId, App* app, MovementComponentPtr movementPtr) : Component(actorId, app), movementPtr(movementPtr){ app->getEventManager()->addListener((EventListenerPtr) this, typeid(CollisionEvent).hash_code());}
BounceComponent inherits from EventListener.
When closing the application I get an error message saying:
Unhandled exception at 0x00f3362c in wPong.exe: 0xC0000005: Access violation reading location 0xfeeefeee.
Probably due to freeing already freed memory.
If I remove the call to addListener() from the constructor and place it in my init() function, right after creating the BounceComponent like this:
typedef std::shared_ptr<BounceComponent> BounceComponentPtr;BounceComponentPtr bounce3(new BounceComponent(3, this, movement3));getEventManager()->addListener(static_cast<EventListenerPtr> (bounce3), typeid(CollisionEvent).hash_code());
everything works.
I guess it's the cast of 'this' to a shared_ptr that's causing the problem. How can I solve this? I'd really like for the object to add it self to the event listener when created.