1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-24 08:32:34 +02:00

Merge pull request #521 from dydzio0614/BattleMemoryLeak

Fix creature animation callback circular reference
This commit is contained in:
Alexander Shishkin 2018-11-15 02:17:20 +03:00 committed by GitHub
commit 2748650a57
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -47,26 +47,30 @@
CondSh<bool> CBattleInterface::animsAreDisplayed(false);
CondSh<BattleAction *> CBattleInterface::givenCommand(nullptr);
static void onAnimationFinished(const CStack *stack, std::shared_ptr<CCreatureAnimation> anim)
static void onAnimationFinished(const CStack *stack, std::weak_ptr<CCreatureAnimation> anim)
{
if (anim->isIdle())
if(anim.expired())
return;
std::shared_ptr<CCreatureAnimation> 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)