Bad access code. Need help.

Bad access code. Need help.
0.0 0


I have some code.

Zombie * **zombie = new Zombie *; //Global variable
Here is the some code of layer class “Level1”
//z[i] = new Zombie;
*zombie[i] = new Zombie;

printf(“So d : f f \n",i,_zombie[i]->z_sprite_sheet->getPositionX(),_zombie[i]->z_sprite_sheet->getPositionY());


And here is the “update” function of Level1 class

void Level1::update(float dt){
    for(int i=0;i\<30;i++){
        printf("So d : f f ”,i,*zombie[i]~~\>z\_sprite\_sheet~~\>getPositionX,*zombie[i]-

In the constructor of Zombie class i assigned the position of `z_sprite_sheet` to `ccp`
And then I ran the code. At the first `printf`, all the results are `128.0000`, `0.0000`. In the update function, the printed results are not `128.0000`, `0.0000`. I didn’t even touch to the position of `z_sprite_sheet`.When i add

to the update function. The error Bad_access_code appeared.

Can anyone explain this to me?


Didn’t you get any errors with this during compilation?

You create an array of 30 Zombies, but it seems from the rest of your code that you want an array of 30 pointers to Zombies.
If so you should do it like that :

Zombie **_zombie = new Zombie*[30];

for (int i = 0; i < 30; ++i) {
    _zombie[i] = new Zombie;

What your line does ( @ Zombie zombie = new Zombie[30]; @ ) is it creates a pointer to Zombie. But in C++ a pointer can also mean an array . So your code is equivalent to @ Zombiezombie[30]; @ - it creates all 30 Zombies in this line.
I’d think that a compiler should throw an error inside your “for” loop on line _zombie[i] = new Zombie; because you are trying to assign
Zombie to Zombie, and these are not the same (obviously) and there is no default conversion between them. You could do it like that : _zombie[i] = *(new Zombie); - this way you dereference the pointer, but this would be a HUGE mistake, as it would end in memory leaks (you’d loose access to the first 30 Zombies).

What my snippet does it creates an array for 30 POINTERS to Zombies, so that you can safely “new” them.

Also, the correct way to clean both cases would be :
Your code :
@ delete [] zombie; @
My code :
for {

delete [] _zombie;

Summing up : I’d suggest trying with the code snippet I posted above.

Hope I could help!


Thank you for your answer :). You are right, it should be " Zombie * * *zombie = new Zombie * ; “. Actually i use this in my code, but mistyped in the question. Sorry!
The”Bad_access_code" error like above is in the case of " Zombie * **zombie = new Zombie * [30] " , not " Zombie *_zombie = new Zombie [30]". Do you have any ideal about this?


Does Zombie inherit from CCObject or other Cocos class? If so do you add it as a Child to something?
If it does inherit, try doing _zombie[i]->retain();


No, Zombie doesn’t inherit from any class. And retain() is a private method of CCObject, i think Zombie cannot use it.


Check that z_sprite_sheet is not deleted. Cocos2d objects created with create() function are autoreleased by default. If object is autoreleased and have no more refs, it will be deleted when your code returns control back to cocos2d. It happens before CCScene::update() method called.


That’s what I meant with checking the retain() on Zombie :slight_smile: As I have never worked with z_sprite_sheet I thought it may be a part of a cocos class from which you inherited, thus assuming you can call retain.


Oh it works after i call _zombie~~>[ i ]~~>z_sprite_sheet-> retain(). Thank you !