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 6603997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:05:32+00:00 2026-05-25T19:05:32+00:00

This is for an iPhone APP written in Objective-C using Xcode 4. The quick

  • 0

This is for an iPhone APP written in Objective-C using Xcode 4.

The quick question is if you have a function which returns an NSArray which was ALLOC’ed in that function, do you have to release it?

Here is the more detailed question.

I run “Analyse” on my iPhone app and it complains about a possible memory leak on one of my functions

The function creates a NSArray out of a NSMutableArray and returns the NSArray. What I am doing is taking an NSMutableArray of class objects and creating an NSArray of NSStrings out of them. I have simplified the code as much as possible to show the problem so don’t worry if it looks like its not doing anything useful.

-(NSArray *)encodeArray
{
    // I use a NSMutableArray here because I do not know how big the starting
    // array will be (I hard coded the 20 here for now)
    NSMutableArray *tmp = [[NSMutableArray alloc]init ];

    for (int y = 0;y<20;y++) {
        // create the NSString object and add it to the tmp array
        NSString *cardcount = [NSString stringWithFormat:@"%i%",y];
        [tmp addObject:cardcount];
    }
    // create the array we will be returning out of the NSMutableArray
    NSArray *array = [[NSArray alloc] initWithArray:tmp copyItems:YES];
    // release the tmp array we created.
    [tmp release];

    // return our array
    // This is the location of the potential memory leak.  SHOULD I RELEASE THIS
    // If I DO - HOW DO I RETURN IT.
    return array;
}

Do I need to release the array? If so, how can I still return it? Maybe there is a better way to perform what I am doing?

The overall goal is to create a NSArray so that I can use the NSUserDefaults to save the application state.

  • 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-25T19:05:33+00:00Added an answer on May 25, 2026 at 7:05 pm

    As a rule of thumb; if you (by you I mean the current scope of the object) retain/copy/etc an object, then you must release/autorelease it somewhere. Since whoever calls encodeArray didn’t do the retaining on array, they are not responsible for releasing it. Therefore, it needs to be set to be autoreleased before being returned:

    -(NSArray *)encodeArray
    {
        // I use a NSMutableArray here because I do not know how big the starting
        // array will be (I hard coded the 20 here for now)
        NSMutableArray *tmp = [[NSMutableArray alloc] init];
    
        for (int y = 0;y<20;y++) {
            // create the NSString object and add it to the tmp array
            NSString *cardcount = [NSString stringWithFormat:@"%i%",y];
            [tmp addObject:cardcount];
        }
    
        // create the array we will be returning out of the NSMutableArray
        // Named initializers indicate that the object will be autoreleased:
        NSArray *array = [NSArray arrayWithArray:tmp];
    
        // release the tmp array we created.
        [tmp release];
    
        // return our array
        return array;
    }
    

    Or:

    -(NSArray *)encodeArray
    {
        // I use a NSMutableArray here because I do not know how big the starting
        // array will be (I hard coded the 20 here for now)
        NSMutableArray *tmp = [[NSMutableArray alloc] init];
    
        for (int y = 0;y<20;y++) {
            // create the NSString object and add it to the tmp array
            NSString *cardcount = [NSString stringWithFormat:@"%i%",y];
            [tmp addObject:cardcount];
        }
        // create the array we will be returning out of the NSMutableArray
        NSArray *array = [[NSArray alloc] initWithArray:tmp];
    
        // release the tmp array we created.
        [tmp release];
    
        // return our array
        return [array autorelease];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question discusses encrypting data on the iPhone using the crypt() function. As an
I'm making a iPhone App written in Objective-C using the Cocos2d game engine. I'm
I'm working on an iPhone app using objective C. I've got class A, which
When building a new iPhone app in Xcode, I'm getting this error: codesign error:
I'd like to use an STL set in my iPhone app (which is written
I have written an iPhone app that streams several radio stations. The app works
I've written a fairly basic app for the iPhone, which I would like to
I have this iPhone app that has an array containing around 50 to 100
This is not a technical question, but given that there are a few iPhone
Although I don't have an iPhone to test this out, my colleague told me

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.