Memory management in cocos2d-x c++

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 : public Node
{
public :
virtual ~ProductBody();
ProductBody();
static ProductBody* create();
// another several functions here
}

ProductBody.cpp

ProductBody::~ProductBody() {}
ProductBody::ProductBody() {}

ProductBody* ProductBody::create()
{
ProductBody* ret = new ProductBody();
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();
static FighterAnimation* 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 = new FighterAnimation();
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 = new FightDragon();
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

the code you posted isn’t formatted so well. Perhaps I am missing here you call ::loadAnimation?

@slackmoehrle I am sorry for missing formation of my code.
The code segment and the classes are just sample, overall classes are so big that’s why I can’t post the full class.
By the way ::loadAnimation called from FightDragon.cpp itself several times when a Dragon is killed and come another Dragon in the battle field.

Thanks for reply, please suggest me what can I do.

I suggest you post more relevant snippets :slight_smile: We don’t have time to imagine what you might be doing versus what you write that you are doing. Show us, please :slight_smile:

Hi,
If I 've Understood , You called Create() of those animation classes, so it will call new() of the classes. They will be constructed from scratch, so the value of the pointer you are printing will of course be different, and all internal variables will be set with the initial values.

So, so far, so normal !

If you need to re use the same the instances of the classes, I would advice you to create an object pool. Google it , you will understand.

your instances are flagged as autorelease but you dont retain them in your loadAnimation function.
(release them later when you dont need them anymore)