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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:33:32+00:00 2026-05-12T06:33:32+00:00

Here is code I am referring to. // Person.h @interface Person : NSObject {

  • 0

Here is code I am referring to.

// Person.h

@interface Person : NSObject {
    NSString *firstName;
    NSString *lastName;
}
@end

// Person.m

@implementation Person
- (id)init {
    if (![super init]) return nil;
    firstName = @"John";
    lastName = @"Doe";
}
@end

// MyClass.m

@implementation MyClass
    .....
- (NSArray *)getPeople {
    NSMutableArray *array = [[NSMutableArray alloc] init];

    int i;
    for (i = 0; i < 10; i++) {
        Person *p = [[Person alloc] init];
        [array addObject:p];
    }

    return array;
}
    .....
@end

Now, I know there is no memory-management going on in this sample code. What would be required?

In the getPeople loop, I am alloc’ing a Person (retainCount 1), then adding it to array. The retain count is now 2, right? If it is two, should I be [p release]’ing after adding it to the array, bringing the retainCount back down to 1?

Am I right in that it is the caller’s responsibility to release the array returned by the method? (Which would also free the memory of the Person’s, and their instance variables, assuming their counts are at 1).

I have read Apple’s memory management document, but I guess what I am most unclear about, is what increases an objects retain count? I think I grasp the idea of who’s responsibility it is to release, though. This is the fundamental rule, according to Apple:

You take ownership of an object if you create it using a method whose name begins with “alloc” or “new” or contains “copy” (for example, alloc, newObject, or mutableCopy), or if you send it a retain message. You are responsible for relinquishing ownership of objects you own using release or autorelease. Any other time you receive an object, you must not release it.

bobDevil’s sentence “only worry about the retain counts you add to the item explicitly” made it click for me. After reading the Ownership policy at Apple, essentially, the object/method that created the new object, is the one responsible for releasing /it’s/ interest in it. Is this correct?

Now, let’s say I a method, that receives an object, and assigns it to a instance variable. I need to retain the received object correct, as I still have an interest in it?

If any of this is incorrect, let me know.

  • 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-12T06:33:33+00:00Added an answer on May 12, 2026 at 6:33 am

    You are correct that the retain count is 2 after adding it to an array. However, you should only worry about the retain counts you add to the item explicitly.

    Retaining an object is a contract that says “I’m not done with you, don’t go away.” A basic rule of thumb (there are exceptions, but they are usually documented) is that you own the object when you alloc an object, or create a copy. This means you’re given the object with a retain count of 1(not autoreleased). In those two cases, you should release it when you are done. Additionally, if you ever explicitly retain an object, you must release it.

    So, to be specific to your example, when you create the Person, you have one retain count on it. You add it to an array (which does whatever with it, you don’t care) and then you’re done with the Person, so you release it:

    Person *p = [[Person alloc] init]; //retain 1, for you
    [array addObject:p]; //array deals with p however it wants
    [p release]; //you're done, so release it
    

    Also, as I said above, you only own the object during alloc or copy generally, so to be consistent with that on the other side of things, you should return the array autoreleased, so that the caller of the getPeople method does not own it.

    return [array autorelease];
    

    Edit:
    Correct, if you create it, you must release it. If you invest interest in it (through retain) you must release it.

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

Sidebar

Ask A Question

Stats

  • Questions 146k
  • Answers 146k
  • 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 SELECT t1.id FROM mytable1 t1 --SPECIFY JOINs HERE inner join… May 12, 2026 at 9:00 am
  • Editorial Team
    Editorial Team added an answer You have to first decode the quoted-printable encoding. This is… May 12, 2026 at 9:00 am
  • Editorial Team
    Editorial Team added an answer You must specify a padding mechanism when you call Cipher.getInstance()… May 12, 2026 at 9:00 am

Related Questions

I'm writing a straightforward C program on Linux and wish to use an existing
I'm running a Tomcat server with some virtual hosts and I need to POST
I am referring to exception messages that show the developer is incorrectly using an
I am trying to use a malloc of short, something like typedef union _SOME_STRUCT_
At school we have been using a bootstrap program to run stand-alone programs without

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.