Error with "Learn Cocos2d Game Development with iOS5" game porting (chapter 4 - spider's game)

Hi
I’m porting example from chapter 04 from “Learn Cocos2d Game Development with iOS5” book (this one with spiders)
But on update method: spidersUpdate on line 113 (source in attachment)
int randomSpiderIndex = (int)CCRANDOM_0_1() * spiders->count();

“Unhandled exception at 0x5506d279 (libcocos2d.dll) in DoodleDrop.win32.exe: 0xC0000005: Access violation reading location 0xfeeefeee.”

It looks like spiders CCArray object is empty. But WHY ??? It’s working fine with “player” object.
Any suggest?


HelloWorldScene.cpp.zip (1.2 KB)

OK - I replace CCArray with CCMutableArray<CCSprite*>() and it works.

I got the same question.

why the CCArray can’t working, replaced it to CCMutableArray<CCSprite*>() ,and it works.

I got the same question.

why the CCArray can’t work, replaced it to CCMutableArray<CCSprite*>() ,and it works.

I have the same problem, but when I need to change the code?
because there many CCArray’s in this code.

Can someone post the code based in the attachment make by ‘Przemo Koczi’

OK, i have solved the mistery :slight_smile:
All explains is here: http://www.cocos2d-x.org/projects/cocos2d-x/wiki/Memory_Management_in_Cocos2d-x
Line below will fix this issue (inserted on line number 53):

spiders->retain();

When you do something like this:

spiders = CCArray::arrayWithCapacity(numSpiders);

method arrayWithCapacity automatically put the spiders to autorealease pool. It’s mean that he takes responsibility of release this object (and delete it at the end)
So as soon as possible he will release this object - that’s why we must inform the engine that we will need this spiders later. That’s why we call retain method.

But why in this part:

CCSprite *sprite = CCSprite::spriteWithFile("spider.png");

we don’t need to call retain method? Because of this:

this->addChild(sprite, 0, 2);

addChild will retain object for us.

thanks for your explain
it help me to understand better cocos2d-x