CCArray is empty , after i do add elements


#1

hey , well i probably miss here something but i have first function that add element to CCArray
but when i try to access it in my update function that is invoked via schedule its NOT null but empty .

the CCArray is created in the init function and defined as class member like this :

this is defined in the header of HelloWorld class :
protected: cocos2d::CCArray *groundSprites;

`void HelloWorld::appendGround(CCSprite* ground)
{
if(ground==NULL)
{
ground = CCSprite::create(“ground.png”);
ground->setScale(0.8);
}

if(groundSprites==NULL)
{
    groundSprites = cocos2d::CCArray::create();
}

int x = 0;
if(groundSprites->count()==0)
{
    x = ground->getTextureRect().size.width * ground->getScale() /2 ;
}
else
{
    CCSprite *lastGround = (CCSprite*)groundSprites->lastObject();
    x = lastGround->getPosition().x + lastGround->getTextureRect().size.width * lastGround->getScale();
}


int y = ground->getTextureRect().size.height * ground->getScale() /2;
CCPoint pos = ccp(x,y);
ground->setPosition(pos);
groundSprites->addObject(ground);
int c = groundSprites->count();  //This is = 3 
CCNode *node = ground->getParent();
if(node==NULL)
{
    this->addChild(ground,ZIndexGround);
}

}
`

appendGround ground is called first before the schedule:

`bool HelloWorld::init()
{

groundSprites =NULL;
spritSheet =NULL;
player =NULL;
jumpAnimation =NULL;
runAnimation =NULL;



bool bRet = false;
do 
{
    //////////////////////////////////////////////////////////////////////////
    // super init first
    //////////////////////////////////////////////////////////////////////////

    CC_BREAK_IF(! CCLayer::init());

    //////////////////////////////////////////////////////////////////////////
    // add your codes below...
    //////////////////////////////////////////////////////////////////////////

    // 1. Add a menu item with "X" image, which is clicked to quit the program.

    // Create a "close" menu item with close icon, it's an auto release object.
    CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
        "CloseNormal.png",
        "CloseSelected.png",
        this,
        menu_selector(HelloWorld::menuCloseCallback));
    CC_BREAK_IF(! pCloseItem);

    // Place the menu item bottom-right conner.
    pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20));

    // Create a menu with the "close" menu item, it's an auto release object.
    CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
    pMenu->setPosition(CCPointZero);
    CC_BREAK_IF(! pMenu);

    // Add the menu to HelloWorld layer as a child layer.
    this->addChild(pMenu, 1);

    appendGround(NULL);
        appendGround(NULL);
        appendGround(NULL);

    this->schedule(schedule_selector(HelloWorld::update),1.0);


    bRet = true;
} while (0);

return bRet;

}`

and then when the update function invoked it passing the
groundSprites=NULL
but failed in the:
int c = groundSprites->count();
CCARRAY_FOREACH(groundSprites,it)

although in the appendGround its value is 3 .

`void HelloWorld::update(float delta)
{

float speed = 500.0;
float deltaX = speed * delta;
CCObject *it;
if(groundSprites!=NULL)
{

    if(groundSprites->data!=NULL)
    {
            int c = groundSprites->count();
            CCARRAY_FOREACH(groundSprites,it)
            {

                CCSprite* ground = dynamic_cast<CCSprite*>(it);
                ground->setPosition(ccpAdd(ground->getPosition(),ccp(deltaX,0)));

            }            
    }
}

}
`


#2

solution :
most add groundSprites->retain();
after creating cocos2d::CCArray

[[http://www.cocos2d-x.org/projects/cocos2d-x/wiki/Reference_Count_and_AutoReleasePool_in_Cocos2d-x]]