Note that there are some explanatory texts on larger screens.

plurals
  1. POmemory issue when passing the cclayer to my objects
    primarykey
    data
    text
    <p>I've got a CCLayer where I want to add many buttons. I created the class MyButton with this init function:</p> <pre><code>-(id) initWithLayer:(MyLayer *)theLayer andDictionary:(NSDictionary *)data { self = [super init]; if (self != nil) { self.layer = theLayer; self.tag = [[data objectForKey:@"day"] intValue]; _layerZ = [[data objectForKey:@"z"] intValue]; _closedCard = [[CCSprite alloc] initWithFile:[data objectForKey:@"file"]]; _openedCardFile = [[NSString alloc] initWithString:[data objectForKey:@"open_card"]]; CCMenuItemImage *itemImage = [CCMenuItemImage itemFromNormalSprite:_closedCard selectedSprite:nil target:self selector:@selector(displayCard:)]; CGPoint position = CGPointFromString([data objectForKey:@"position"]); _position = ccp(position.x, position.y); itemImage.position = _position; itemImage.tag = self.tag; _theCardButton = [CCMenu menuWithItems:itemImage, nil]; _theCardButton.position = CGPointZero; [self.layer addChild:_theCardButton z:_layerZ]; } return self; </code></pre> <p>}</p> <p>This init function will add the buttons to the layer and I'm managing all the behaviour (movements, image changes) in the MyButton class.</p> <p>So the layer is being sent to all the MyButton instances. The layer won't call the dealloc method until each button is released.</p> <p>These buttons are being added to the layer through an array:</p> <pre><code>_buttons = [[NSMutableArray alloc] init]; for (NSDictionary *buttonData in buttonsArray) { MyButton *btn = [[MyButton alloc] initWithLayer:self andDictionary:buttonData]; [_buttons addObject:btn]; [btn release]; } </code></pre> <p>My problem is that when I replace this scene, the dealloc methos of MyButton is not being called and the memory is not being released, cause the [_buttons release] is in the dealloc method of the layer, which is not being called either, cause the layer has been passed to each button.</p> <p>It's a mess. Can anyone tell me the best way for fixing this memory problem?</p> <p>-- edit</p> <p>If I try to release the array of buttons manually before calling the replaceScene, each button calls the dealloc, but then I get this error message:</p> <pre><code>[CCMenu pauseSchedulerAndActions]: message sent to deallocated instance 0x131a0670 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload