From 0fd729fd8672f8a2ce04dc75e4c71c2b21729b2a Mon Sep 17 00:00:00 2001 From: Dydzio Date: Wed, 14 Nov 2018 23:10:55 +0100 Subject: [PATCH] Fix creature animation callback circular reference --- client/battle/CBattleInterface.cpp | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/client/battle/CBattleInterface.cpp b/client/battle/CBattleInterface.cpp index 1fa1bd084..c2e986e0d 100644 --- a/client/battle/CBattleInterface.cpp +++ b/client/battle/CBattleInterface.cpp @@ -47,26 +47,30 @@ CondSh CBattleInterface::animsAreDisplayed(false); CondSh CBattleInterface::givenCommand(nullptr); -static void onAnimationFinished(const CStack *stack, std::shared_ptr anim) +static void onAnimationFinished(const CStack *stack, std::weak_ptr anim) { - if (anim->isIdle()) + if(anim.expired()) + return; + + std::shared_ptr animation = anim.lock(); + if (animation->isIdle()) { const CCreature *creature = stack->getCreature(); - if (anim->framesInGroup(CCreatureAnim::MOUSEON) > 0) + if (animation->framesInGroup(CCreatureAnim::MOUSEON) > 0) { if (CRandomGenerator::getDefault().nextDouble(99.0) < creature->animation.timeBetweenFidgets *10) - anim->playOnce(CCreatureAnim::MOUSEON); + animation->playOnce(CCreatureAnim::MOUSEON); else - anim->setType(CCreatureAnim::HOLDING); + animation->setType(CCreatureAnim::HOLDING); } else { - anim->setType(CCreatureAnim::HOLDING); + animation->setType(CCreatureAnim::HOLDING); } } // always reset callback - anim->onAnimationReset += std::bind(&onAnimationFinished, stack, anim); + animation->onAnimationReset += std::bind(&onAnimationFinished, stack, anim); } static void transformPalette(SDL_Surface *surf, double rCor, double gCor, double bCor)