Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8253779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:59:31+00:00 2026-06-08T00:59:31+00:00

I’ve got a CCLayer where I want to add many buttons. I created the

  • 0

I’ve got a CCLayer where I want to add many buttons.
I created the class MyButton with this init function:

-(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;

}

This init function will add the buttons to the layer and I’m managing all the behaviour (movements, image changes) in the MyButton class.

So the layer is being sent to all the MyButton instances. The layer won’t call the dealloc method until each button is released.

These buttons are being added to the layer through an array:

_buttons = [[NSMutableArray alloc] init];
for (NSDictionary *buttonData in buttonsArray) {
   MyButton *btn = [[MyButton alloc] initWithLayer:self andDictionary:buttonData];
   [_buttons addObject:btn];
   [btn release];
}

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.

It’s a mess. Can anyone tell me the best way for fixing this memory problem?

— edit

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:

[CCMenu pauseSchedulerAndActions]: message sent to deallocated instance 0x131a0670
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T00:59:33+00:00Added an answer on June 8, 2026 at 12:59 am

    Do you retain the layer in your initWithLayer:andDictionary: method? Then you have a retain cycle.

    When you send retain message to an object, you increase its retain count by 1. When you send release message, you decrease retain count by 1. dealloc message is sent to object when its retain count reaches 0.

    When release message is sent to your layer, its retain count does not reach 0, because every one of _buttons retained it for itself. Because layer’s retain count never reaches 0, its dealloc is never called and buttons never get a release message, never get their dealloc called, never release the layer and so on. Cycle.

    To avoid retain cycles, a child object must never retain any of its parents, or any of parents’ parents.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I want to count how many characters a certain string has in PHP, but
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I want to construct a data frame in an Rcpp function, but when I
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.