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

  • Home
  • SEARCH
  • 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 6194895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:22:23+00:00 2026-05-24T03:22:23+00:00

I am using cocos2d to populate a NSMutable Array, then creating an NSArray from

  • 0

I am using cocos2d to populate a NSMutable Array, then creating an NSArray from that array. I do the following code 3 times in a row with different array names, and on the third time Instruments is reporting leaks with each element I add to the array.

The weird thing is, its not on EVERY creation and addition of a CCSprite, and the lines that it complains about are different every time I run the app. What am I doing wrong? Is there a better way of doing this?

Here is my code:

NSMutableArray *tempNumberArray = [[NSMutableArray alloc] init];

tempSprite = [[CCSprite alloc] initWithSpriteFrameName:@"0.png"];
[tempNumberArray addObject:tempSprite];
[tempSprite release];
tempSprite = nil;


tempSprite = [[CCSprite alloc] initWithSpriteFrameName:@"0.png"];
[tempNumberArray addObject:tempSprite];
[tempSprite release];
tempSprite = nil;

tempSprite = [[CCSprite alloc] initWithSpriteFrameName:@"0.png"];
[tempNumberArray addObject:tempSprite];
[tempSprite release];
tempSprite = nil;

tempSprite = [[CCSprite alloc] initWithSpriteFrameName:@"0.png"];
[tempNumberArray addObject:tempSprite];
[tempSprite release]; 
tempSprite = nil;

self.numbersArray = [NSArray arrayWithArray:tempNumberArray];
[tempNumberArray release];
tempNumberArray = nil;

Edit: Thanks for taking a look at this.
The first time I use tempSprite I initialize it like:

CCSprite * tempSprite = [[CCSprite alloc] initWithSpriteFrameName:@"0.png"];
[tempNumberArray addObject:tempSprite];
[tempSprite release]; 
tempSprite = nil;

I release tempSprite between each allocation because it would be a leak otherwise. [tempNumberArray addObject:tempSprite] retains the sprite object.

  • 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-05-24T03:22:24+00:00Added an answer on May 24, 2026 at 3:22 am

    I’m not sure why you’re seeing leaks. The code you’ve posted is correct, although it’s not necessary to set tempSprite to nil every time; you really only need to do this if there is a chance you will use the pointer to try to message the object after releasing it. It doesn’t hurt anything, however.

    The only improvement I can suggest is to do the array construction in a loop:

    // You can also use an autoreleased mutable array, since you don't need it
    // to stick around after construction.
    NSMutableArray * tempNumbersArray = [NSMutableArray array];
    int i;
    for( i = 0; i < NUM_OF_SPRITES; i++ ){
        CCSprite * tempSprite = [[CCSprite alloc] initWithSpriteFrameName:@"0.png"];
        [tempNumbersArray addObject:tempSprite];
        [tempSprite release];
    }
    
    self.numbersArray = [NSArray arrayWithArray:tempNumbersArray];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using cocos2d with objective C. I have a class called CrystalineBubble that is
I am using cocos2d but there is a chance its not that which is
I am creating a game using cocos2d. Now what I want is I need
I'm developing a game for iPhone using COCOS2D . In that, I need to
I'm using cocos2d-x to develop an iPhone game and then it just came to
(I'm using cocos2d for iphone) First I'll explain you my code: (the important parts)
I am creating a iphone game currently using cocos2d. The basics of the game
I am using cocos2d-iphone to place Sprites onto a Layer to setup a game
I am trying to develop a iphone app by using cocos2d. I create a
Using ASP.NET MVC there are situations (such as form submission) that may require a

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.