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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:28:50+00:00 2026-05-26T21:28:50+00:00

In objective-c manual it is written that something like return [[[SomeClass alloc] init] autorelease];

  • 0

In objective-c manual it is written that something like

return [[[SomeClass alloc] init] autorelease];

can be done and then release is not necessary at any point, even not in the function that received this object. Who owns this object then? when will it be released?

  • 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-26T21:28:50+00:00Added an answer on May 26, 2026 at 9:28 pm

    The current NSAutoreleasePool does and will take care of the releasing when drained.

    IBAction calls get wrapped into an NSAutoreleasePool that gets drained after the call.

    For all non-IBAction calls the following would apply:

    Say, you have these methods:

    - (id)foo {
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
        SomeClass *bar = [self bar]; //bar still exists here
        //do other stuff
        //bar still exists here
        //do other stuff
        //bar still exists here
        [pool drain]; //bar gets released right here!
        //bar has been released
    }
    
    - (id)bar {
        return [[[SomeClass alloc] init] autorelease]; //bar will still be around after the return
    }
    

    Consider another scenario:

    - (void)foo {
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
        //do other stuff
        [self bar];
        //do other stuff
        [pool drain]; //the string from blee would be released right here;
    }
    
    - (void)bar {
        [self baz];
    }
    
    - (void)baz {
        NSString *string = [self blee];
    }
    
    - (id)blee {
        return [NSString string]; //autoreleased
    }
    

    As you can see the autoreleased string object did not even have to be used in or get returned to the scope in which the pool was created.

    NSAutoreleasePools exist on a stack (one per thread) and autoreleased objects get owned by the pool that’s topmost at the time of the call to autorelease.


    Update:
    If you are dealing with a tight loop and want to keep the memory moderately low while not slowing down your loop, consider doing something like this:

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    for (NSUInteger i = 0; i < 1000000000; i++) {
        [NSString stringWithString:@"foo"];
        if (i % 1000 == 0) {
            [pool drain];
            pool = [[NSAutoreleasePool alloc] init];
        }
    }
    [pool drain];
    

    However NSAutoreleasePool is highly optimized, so one pool per iteration usually isn’t much of a problem.


    To fully understand how NSAutoreleasePools work read this excellent article by Mike Ash

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

Sidebar

Related Questions

Objective: Be able to nest a resource, like records inside of users so that
I discovered something unfamiliar while reading Objective-C manual for @encoding . Table 6-2 Objective-C
I am confused with releasing the object that I first alloc/initialized and then copied.
GCC manual says: file.m      Objective-C source code. Note that you must link with
Objective-C has no namespaces; it's much like C, everything is within one global namespace.
Objective : Make a progress bar where users can check how much of a
Objective: In support of a Windows Service that may have multiple instances on a
Objective Have a small magnifying glass icon that appears in the top right corner
Objective: I would like to reset the array attribute of all objects in a
In Objective-C 2.0, I usually make an assign property for ivars that are primitive

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.