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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:39:41+00:00 2026-05-20T10:39:41+00:00

Im trying to understand when to call autorelease, and what this will actually do

  • 0

Im trying to understand when to call autorelease, and what this will actually do to my object.

After reading About Memory Management in the Mac Developer Library I understood that when you have a method that acts as a factory method – by creating a new object and returning it – the method has no way of releasing the object before returning it, because this would result in a deallocted object being returned.

Example

- (Test *) createNewTest 
{
    Test *newInstance = [[Test alloc] init];
    [newInstance release];
    return newInstance; // deallocted object returned.
}

Instead I should use autorelease:

The autorelease method, defined by NSObject, marks the receiver for later release

My question is: if the object is to be released later, how do I know when its being released?

- (Test *) createNewTest 
{
    Test *newInstance = [[test alloc] init];
    [newInstance autorelease];
    return newInstance;
}

- (void) runIt
{
    Test *myInstance = [self createNewTest];
    // when is myInstance released?? and thereby not valid to my function anymore?
}

How can I safely use the returned autoreleased object inside my runIt method if I don’t know when the autorelease happens? Should I retain the object returned by the createNewTest? Or can I safely use it within the runIt scope?

  • 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-20T10:39:42+00:00Added an answer on May 20, 2026 at 10:39 am

    An autoreleased object is added to an autorelease pool.

    Objects in the autorelease pool are released at the end of an iteration of the main runloop (or sooner if you are managing your own autorelease pool and/or if you call drain).

    When you call a method that returns an autoreleased object, it is pretty much guaranteed to stay valid until at least the end of the scope that it was called in.

    If you want to ensure that it stays alive longer then you should retain it, remembering to release it again when you’re finished with it.

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

Sidebar

Related Questions

I'm learning about the call()-method in Javascript and trying to understand this ex. Now
I was trying to understand this call: deprecate :new_record?, :new? which uses this deprecate
I am trying to understand how CLR dispatches method call correctly when an object
I'm trying to understand why the author of this function would call reduce on
I'm trying to understand when I should call Query.close(Object) or Query.closeAll(); From the docs
I am trying to understand more about the Date object in javascript. I thought
I am trying to understand how mmap works. User level call of mmap looks
Trying to understand PNG format. Consider this PNG Image: The Image is taken from
Trying to understand Ruby a bit better, I ran into this code surfing the
Trying to understand the math of this code snippet. A token is provided which

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.