Hi
I am working on cocos2d-x c++(v3.16)
. I am facing a horrible problem related to memory management. Let’s describe the problem :-
I have a class like SkeletonAnimation
of cocos2d-x
library named ProductBody
inherited from Node which is used to animate Products(Dragons, Dinos etc). I have another class named FighterAnimation
inherited from ProductBody
.
I have another class named FightDragon
where the dragons do fight against opponent dragons.
In FightDragon
class I create two objects of FighterAnimation
are deathAnimation
and powerAnimation
. I have to remove deathAnimation
and powerAnimation
after the animation sequence done. After that deathAnimation
and powerAnimation
is created. In this loop I notice that deathAnimation
and powerAnimation
is altered. This is the problem. I don’t know why this is happening.
My classes are below :-
ProductBody.h
class
ProductBody
: publicNode
{
public :
virtual ~ProductBody
();
ProductBody
();
static ProductBody* create();
// another several functions here
}
ProductBody.cpp
ProductBody
::~ProductBody
() {}
ProductBody
::ProductBody
() {}
ProductBody
*ProductBody
::create()
{
ProductBody
* ret = newProductBody
();
if(ret && ret->init()) {
ret->autorelease();
return ret;
} else {
delete ret;
ret = nullptr;
return nullptr;
}
}
`
FighterAnimation.h
class
FighterAnimation
: public ProductBody
{
private :
virtual ~FighterAnimation
();
FighterAnimation();
staticFighterAnimation
* create( int product, int mylevel, bool movement);
// another several functions here
}
FighterAnimation.cpp
FighterAnimation
::~FighterAnimation
() {}
FighterAnimation
::FighterAnimation
() {}
FighterAnimation
*FighterAnimation
::create( int product, int mylevel, bool movement)
{
FighterAnimation
* ret = newFighterAnimation
();
if (ret && ret->init(product, mylevel, movement)) {
ret->autorelease();
return ret;
} else {
delete ret;
ret = nullptr ;
return nullptr ;
}
}
FightDragon.h
class
FightDragon
: public Node
{
public :
~FightDragon
();
FightDragon
();
FighterAnimation
*deathAnimation;
FighterAnimation
*powerAnimation;
void loadAnimation();
}
FightDragon.cpp
FightDragon
::~FightDragon
() {}
FightDragon
::FightDragon
() {}
FightDragon
*FightDragon
::create()
{
FightDragon
* ret = newFightDragon
();
if(ret && ret->init()) {
ret->autorelease();
return ret;
} else {
delete ret;
ret = nullptr;
return nullptr;
}
}void
FightDragon
::loadAnimation()
{
deathAnimation
=FighterAnimation
::create(8202, ageBaby, false , folderName);
powerAnimation
=FighterAnimation
::create(7326,ageAdult, false ,folderName);
}
When I check deathAnimation->productid
it prints 7326 but should prints 8202 and powerAnimation->productid
it prints 8202 but should prints 7326.
In short, these two objects are altered sometimes, I repeat altered sometimes. It means this is not happen all time.
Please help.
Thanks in advance