Where is the memory leak in this code, how does it happen?

Ok, I’ve read through how memory management works with cocos2d-x, and believe I have adequate understanding, however the error below completely perplexes me =s

bool HelloWorld::init()
{

//Code();

#define KNUMASTEROIDS 15
    _asteroids = new CCArray(); 
    for(int i = 0; i < KNUMASTEROIDS; ++i) {
        CCSprite *asteroid = CCSprite::create("asteroid.png");
        asteroid->setVisible(false); <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Program crashes here!
        _batchNode->addChild(asteroid);
        _asteroids->addObject(asteroid);
    }

    return true;
}

After a quick look I can see that you are declaring ‘new’ for your _asteroids array and never deleting it, so that would cause a leak. If I remember correctly CCArray has a create method that would make it an autorelease object.

As for the crash comment, I’m not sure why this happens but try using asteroid->setOpacity(0) this should make it invisible.

Hope this helps.

I understand that there’s 4 keywords that gives me the control to releasing the object, but the program crashes on that line of code, on the very first loop, the object ‘asteroid’ seems to have been destroyed

I tried changing it to setOpacity(0), and the same error occurs, I’m guessing that ‘asteroid’ has already been released and destroyed but how?

I missed this part:-

change :-
CCSprite asteroid = CCSprite::create;
to :-
CCSpriteasteroid = CCSprite::createWithSpriteFrameName(“asteroid.png”)

I don’t know why your way didn’t work, but it didn’t work for me either so I had to change it.

Wow, it does work but now I want to know the purpose of ‘create()’, lol :smiley:

CCSprite::create(“asteroid.png”); is for creating a sprite with file. It will return NULL if this file does not exists so I believe that you do not have a file named “asteroid.png” and this is why you have a crash in next line. If you are using spritesheets then you should use CCSprite::createWithSpriteFrameName(“asteroid.png”) where parameter is the name of the frame on spritesheet.

Hmm thanks!

I found the problem, it seems like a bug with xcode, it doesn’t add all the files into the file navigator, even though it exists in the folder.

If the folder is a group, you will have to add it manually.
On Xcode, when you add a file, check “Create folder references for added folders”. That would make references to the folders and will automatically update when you add files in that folder.

@Lance Gray

Yup, I did that, along with the ‘Copy Bundle Resources’ but it just doesn’t show up, I ended up having to revert to an earlier version and made changes starting from there =s

Also, its a good practice to check the validity of the pointer after create().
i.e.
CCSprite *asteroid = CCSprite::createWithSpriteFrameName(“asteroid.png”);
if (asteroid)
{
do something with asteroid

}

I prefer to let it error if something that should exist is NULL.
That way, you can see it failed to load the sprite, other wise you might be looking at the rest of the code to see what’s going wrong.

I agree that if it could legitimately be NULL then the if is required.