Problem with correct retain usage

Hi.

I have a Game Character with N parts animated. To make it work, I follow the next steps:

1º Create Object
2º Call N times function addpart()
3º Set Movement

The problem is that a error box appear each new Object is created, one for each part that I am adding.

The function addpart() is :

    // Create Animation
    CCAnimation *animation = this->loadPlistForAnimationWithName(initialAnimation, "animationsCharacters.plist");
    animation->retain();  // +1 retain

    // Create Sprite Part with Animation
    CCSprite *spritePart = CCSprite::createWithSpriteFrameName(initialImage);
    spritePart->runAction(CCRepeatForever::create(CCAnimate::create(animation)));
    spritePart->setAnchorPoint(anchor);
    spritePart->setPosition(ccpAdd(spritePart->getPosition(), offset));
    //spritePart->retain();  // I try to use this line to correct the problem, but it don 't work

    // Store info of this character part in Array of parts
    CharacterPart *part = this->makeCharacterPart(part, offset, initialImage, spritePart, initialAnimation, animation);
    this->addChild(spritePart,z);
    partsArray->addObject((CCObject *)part);

    // RETAIN BOX APPEAR HERE!
  • makeCharacterPart just store in a struct the info that I want to Store.

  • If I comment last 3 lines, the box don’t appear, but I want to store the info to change animations between states, etc.

  • It work fine in iOS, and work propertly on Android, but the assert error appear all time.

The error is:

Assert Error, CCObject.cpp function retain line:86

I assume that I using retain more times that I need, or less times, but I don’t know what is going on.

I already have read: http://www.cocos2d-x.org/projects/cocos2d-x/wiki/Memory_Management_in_Cocos2d-x But I don’t understand what is going on.

Some ideas? Any explanation why this issue occurs and what I must consider to manage animated objects?

Thanks in advance.