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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T11:47:29+00:00 2026-05-19T11:47:29+00:00

I am sorta new on Objective-C and I am trying to understand some stuff

  • 0

I am sorta new on Objective-C and I am trying to understand some stuff about this retain release mechanism.

Suppose I need an array to last the entire life of the application. So, suppose I create the array using something like

myArray = [[NSMutableArray alloc] init];

at the beginning of the app.

During the app, this array may have all objects removed, added objects from other arrays, etc. Suppose also, that during one of these operations of adding objects, I add autoreleased objects to the array. Two questions:

  1. will the objects added to that array always be live and never deallocated while the array is allocated?

  2. I know that adding an object to an array will increase its retain count. Is this also valid for autoreleased objects? (perhaps autoreleased arrays coming from other methods)

thanks

  • 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-19T11:47:30+00:00Added an answer on May 19, 2026 at 11:47 am

    will the objects added to that array always be live and never deallocated while the array is allocated?

    They will under normal circumstances, i.e., provided you’re not overreleasing them. For instance,

    // take ownership (alloc) followed by relinquish ownership (autorelease)
    // the net result is that this code snippet DOES NOT own someObject
    SomeClass *someObject = [[[SomeClass alloc] init] autorelease];
    
    // myArray takes ownership of someObject
    [myArray addObject:someObject];
    
    // someObject is INCORRECTLY (over)released
    [someObject release];
    

    Considering that no other code has claimed ownership of someObject, it won’t be ‘live’ because it was overreleased.

    I know that adding an object to an array will increase its retain count. Is this also valid for autoreleased objects? (perhaps autoreleased arrays coming from other methods)

    Yes. Collections won’t look at retain counts or autorelease status whilst adding objects, nor they should. An array simply sends -retain to the object being added and hence takes ownership of that object, regardless of other code owning (or not owning) the object.

    The whole point of memory management and object ownership is to consider ownership in a relative way: if a collection needs an object, it’ll take ownership of the object; if the collection is released or the object is removed from the collection, it’ll relinquish ownership of the object. The collection doesn’t care about the object being owned by other objects or code — it is only concerned about its own perspective of owning the object. The same principle should apply to your code.

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

Sidebar

Related Questions

I am new with Linq and I would like to sort some data that
I'm somewhat new to the ASP.NET MVC architecture and I'm trying to sort out
I've never used SOAP before and I'm sort of new to Python. I'm doing
So .NET 3.0/3.5 provides us with lots of new ways to query, sort, and
I've a dictionary object: Dictionary<string, string[]> dictCompanies = new Dictionary<string, string[]>(); How do sort
I want to parse a config file sorta thing, like so: [KEY:Value] [SUBKEY:SubValue] Now
I would like to sort an array in ascending order using C/C++ . The
I often have to sort a dictionary (consisting of keys & values) by value.
Using the same sort command with the same input produces different results on different
How do I sort a list of dictionaries by a specific key's value? Given:

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.