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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:15:15+00:00 2026-05-12T05:15:15+00:00

I have been looking through the questions asked on StackOverflow, but there are so

  • 0

I have been looking through the questions asked on StackOverflow, but there are so many about memory management in Objective-C that I couldn’t find the answer I was looking for.

The question is if it is ok (and recommnded) to call autorelease before adding a newly created object to a collection (like NSMutableArray)? Or should I release it explicitly after adding it. (I know NSMutableArray willl retain the object)

This illustrates my question:

Scenario A (autorelease):

- (void) add {
   // array is an instance of NSMutableArray

   MyClass *obj = [[[MyClass alloc] init] autorelease];

   [array addObject:obj];
}

Scenario B (explicit release):

- (void) add {
   // array is an instance of NSMutableArray

   MyClass *obj = [[MyClass alloc] init];

   [array addObject:obj];

   [obj release];
}

I assume both are correct, but I am not sure, and I sure don’t know what the preffered way is.

Can the Objective-C gurus shed some light on this?

  • 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-12T05:15:15+00:00Added an answer on May 12, 2026 at 5:15 am

    Both are correct and will work as you’re expecting them to.

    I personally prefer to use the latter method, but only because I like to be explicit about when objects get released. By autoreleasing the object, all we’re doing is saying “this object will get released at some arbitrary point in the future.” That means you can put the autoreleased object into the array, destroy the array, and the object might (probably) still exist.

    With the latter method, the object would get destroyed immediately with the array (providing that nothing else has come along and retained it in the meantime). If I’m in a memory-constrained environment (say, the iPhone) where I need to be careful about how much memory I’m using, I’ll use the latter method just so I don’t have so many objects lingering in an NSAutoreleasePool somewhere. If memory usage isn’t a big concern for you (and it usually isn’t for me, either), then either method is totally acceptable.

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

Sidebar

Ask A Question

Stats

  • Questions 163k
  • Answers 163k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If a and b are strings: b.scan(a).length May 12, 2026 at 12:08 pm
  • Editorial Team
    Editorial Team added an answer Is this the situation? aMethod(whatToLoad) { // other stuff x… May 12, 2026 at 12:08 pm
  • Editorial Team
    Editorial Team added an answer A quick look into the PHP manual revealed this: It… May 12, 2026 at 12:08 pm

Related Questions

What would be the preferred way of hiding an element before the page is
I have a question about this question . I posted a reply there but
We are looking for a way to publish articles in different languages, including differences
It is pretty standard practice now for desktop applications to be self-updating. On the
I know this question has been asked a bit before. But looking around I

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.