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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:46:08+00:00 2026-05-13T16:46:08+00:00

I’m fairly new to iPhone development and I’ve hit a roadblock in my understanding

  • 0

I’m fairly new to iPhone development and I’ve hit a roadblock in my understanding of memory management. I’ve read through the Memory Management Guide for Cocoa and have read many, many questions and answers on SO, but have not found a complete answer.

If I have an instance method that creates an object, every example I’ve seen seems to use an autorelease call:

-(NSArray *)findThings {
    NSArray* things = [[[NSArray alloc] init] autorelease];
    // add some lovely things to my shiny new array
    return things;
}

Forgetting the contrived example, everything I’ve read about iPhone dev best practices says that autorelease pools are discouraged, but how can I implement the above example without one? If this method is called many, many times, it feels like I run the risk of clogging the iPhone’s autorelease pool with, well, “things”, which seems to go against the need to keep resource use to a minimum on such a constrained platform.

I considered the following:

-(NSArray *)findThings {
    NSArray* things = [[NSArray alloc] init];
    // add some lovely things to my shiny new array
    [things release];
    return things;
}

But then ‘things’ would have a retain count of zero before it passes to the calling method, so I feel like there’s a significant risk of the object being deallocated between the call to [things release] and the method which calls findThings actually using the result.

I was a bit confused by the rule from the Memory Management Guide which states:

A received object is normally
guaranteed to remain valid within the
method it was received in. (…) That method
may also safely return the object to
its invoker.

I wasn’t sure whether this meant as the writer of an instance method I could safely perform the release without the risk of the object being deallocated until the calling method’s scope ended, or whether as a user of classes in the frameworks provided by apple I could assume that I didn’t have to worry about retain/release/etc for objects I received from those classes as long as the method didn’t have new/init/alloc/copy/whatever in its name.

So to summarise,

  • Can I use release before returning an object instead of autorelease to avoid using an autorelease pool on the iPhone?
  • If not, is there a better pattern for this that doesn’t involve the autorelease pool?
  • Am I missing something fundamental here? It seems like there’s a hole in the docs.

Thanks in advance

  • 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-13T16:46:08+00:00Added an answer on May 13, 2026 at 4:46 pm

    Yes, you’re right autorelease pools are discouraged for iPhone development when they’re not absolutely necessary. They are discouraged because it allows objects to stay in memory longer then necessary this is a problem when developing applications for the iPhone, where memory is scarce. Autoreleased objects don’t get released until it makes it back to the main autorelease pool created in main.c and this could be a long time. (unless you created your own autorelease pool, I’ll talk about this last.)

    A case where an autoreleased object is not necessary:

    NSString *string = [NSString stringWithString:@"hello world"];
    //use string
    

    The first case is more convenient since I don’t have to worry about releasing string later but it could have been easily been replaced with:

    NSString *string = [[NSString alloc] initWithString:@"hello world"];
    // use string
    [string release];
    

    For these cases avoiding the autoreleased object is recommended.


    When you’re passing back objects using autorelease is unavoidable.

    You must use:

    -(NSArray *)findThings {
        NSArray* things = [[[NSArray alloc] init] autorelease];
        // add some lovely things to my shiny new array
        return things;
    }
    

    Since the object is invalid as soon as you call release.

    -(NSArray *)findThings {
        NSArray* things = [[NSArray alloc] init];
        // add some lovely things to my shiny new array
        [things release];
        // things is now in invalid object if you could potentially use things later it should be set to nil to avoid a crash
        things = nil; // sending messages to nil is okay, it won't cause a crash, sending messages to a released object is not okay, it can cause unpredictable crashes.        
        return things;
    }
    

    In cases where you’re using a large amount of autoreleased memory you should create and drain your own autorelease pool so the autorelease memory doesn’t stay around longer then necessary.

    -(void)useLotsOfAutoReleasedObjects {
        NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
        // lots of autoreleased object
        // large autoreleased objects
        [pool drain];  // autorelease objects are released too
    }
    

    [pool drain] also releases the pool, see the NSAutoreleasePool Reference for more information.

    • 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 am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
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
I want use html5's new tag to play a wav file (currently only supported

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.