views:

52

answers:

1

Hi all Im making an app for the iphone using cocos2d and i am trying to figure out the best approach for removing items from a NSmutableArray and from the layer at the same time. What i mean by this is that the objects within the array inherit from ccNode and contain a ccsprite which i have added as a child to the cclayer. The below code is in a cclayer that has the nsmutablearray called bonusicons.

-(void) AddNewBonusIcon: (int) colour :(int) pos{

    BonusIcon *newbonus;


    CGSize winSize = [[CCDirector sharedDirector] winSize];

    int maxX = winSize.width;
    int maxY = winSize.height;

    int posX, posY;

    newbonus = [[BonusIcon alloc] init];


    [newbonus setBonusColour: colour];


    int bonusOffset = 0;

    posX = anchorX;
    posY = anchorY;

    bonusOffset = [bonusIcons count]*([newbonus.bonus_sprite boundingBox].size.width/2 + 12);

    newbonus.bonus_sprite.position = ccp(posX+bonusOffset,posY);



    [newbonus.bonus_sprite setTag:pos];

    [self addChild:newbonus.bonus_sprite];
    [bonusIcons addObject:newbonus ];   
    [newbonus release];


}

This appears to do what i want for adding the objects sprite to screen and adding the objects to the nsmutablearray. Now of course this is probably not the correct way to do it so shout at me if not!

next i try to delete the objects from the array and from the screen. I can delete them from the array with no problems i just do the following

for (int i = INITIAL_BONUSES-1; i>=0; i--) {


    [bonusIcons removeObjectAtIndex:i];

}

this of course leaves the sprites on screen. so how do i approach what i am trying to do so that i can remove both the sprites from screen and the objects from array that the sprite is associated with. I can remove the sprites from the screen by using the tags and typing

[self removeChildByTag:i cleanup:YES]; but then i get errors when trying to remove items from the array . i assume because i have deleted a part of the object already and the dealloc of the ccnode can no longer find the sprite to release? so any pointers/tips etc of how i should be doing this would be much appreciated. I have read a bunch of stuff on memory management which i believe is my current issue but i just dont seem to be getting it right. thanks all

edit: ok since posting this i have removed the sprite dealloc from the ccnode itself and added it to the cclayer above it. This has stopped the crashing so i guess i was right with the problem i was having. I of course do not think the way i solved it is the most ideal way but it will do until i find a better way.

+1  A: 

You don't have it in the code you posted, but your question seems to strongly imply that you are calling dealloc. The only place you should ever call dealloc is [super dealloc] at the end of a class's dealloc method. Calling it on anything but super or in any other place is wrong and will lead to errors about prematurely deallocated objects (because, well, that's what it does).

If this is what you're doing, I strongly suggest you read Apple's memory management guide. It lays out how memory management works in Cocoa very simply yet thoroughly.

Chuck
sorry i ment to say release not dealloc
but problem has been solved now i just realised how i was releasing the sprite in multiple places. it is something ill pay attention to more now that i have fumbled around this for so long