I’m using Cocos2d-x 3.16 on VS2013.
I made a custom class with sprites in it,
which looks like this
struct Weapon
{
private:
public:
Weapon();
~Weapon();
Sprite *base, *barrel, *muzzleFlash;
Sprite *lasMuzzle, *lasBody, *lasImpact;
//other variables (int, float)
};
class Drone
{
private:
Node* scene;
Sprite *Chassis, *Turret, *Shield, *boostAnim;
cocos2d::MotionStreak* MS;
std::vector<Weapon*> Weapons; //struct defined above
//variables and functions below
public:
...
//variables and functions below
};
After deleting drone object, error occurs.
HEAP: Free Heap block XXXXXXXX modified at XXXXXXXX after it was freed
and when I try on the android device, this error occurs.
fatal signal 11 (sigsegv) code 2, fault addr (address)
destructors of Weapon and Drone,
Weapon::~Weapon()
{
if (muzzleFlash != nullptr)
{
muzzleFlash->removeFromParentAndCleanup(true);
}
if (barrel != nullptr)
{
barrel->removeFromParentAndCleanup(true);
}
if (lasMuzzle != nullptr)
{
lasMuzzle->removeFromParentAndCleanup(true);
}
if (lasBody != nullptr)
{
lasBody->removeFromParentAndCleanup(true);
}
if (lasImpact != nullptr)
{
lasImpact->removeFromParentAndCleanup(true);
}
if (base != nullptr)
{
base->removeFromParentAndCleanup(true);
}
}
Drone::~Drone()
{
for (int loop = 0; loop < Weapons.size(); loop++)
{
delete Weapons[loop];
}
Weapons.clear();
if (Shield != nullptr)
{
Shield->removeAllChildrenWithCleanup(true);
Shield->removeFromParentAndCleanup(true);
}
if (boostAnim != nullptr)
{
boostAnim->removeAllChildrenWithCleanup(true);
boostAnim->removeFromParentAndCleanup(true);
}
if (Turret != nullptr)
{
Turret->removeAllChildrenWithCleanup(true);
Turret->removeFromParentAndCleanup(true);
}
if (MS != nullptr)
{
MS->removeFromParentAndCleanup(true);
}
if (Chassis != nullptr)
{
Chassis->removeAllChildrenWithCleanup(true);
Chassis->removeFromParentAndCleanup(true);
}
}
I stored objects in std::list, delete like,
for (auto loop = vRobots.begin(); loop != vRobots.end();)
{
auto bot = *loop;
if (bot->isDestroyed)
{
vRobots.erase(loop++);
if (bot == EnemyBot)
{
endBattle(true);
return;
}
delete bot;
}
else
++loop;
}
Strange thing is, there is Projectile class which is also a custom class,
and deleting projectiles object doesn’t cause problem.
Projectile::~Projectile()
{
delete fHit; //Factory Function. nothing special.
image->removeFromParentAndCleanup(true); //the only sprite in the class.
//CCLOG("Projectile Destruct");
}
Maybe it’s not a good idea to make a class like these?
or maybe I’m blind and missed something? There aren’t any other codes than those really.
I have no idea.