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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:38:07+00:00 2026-06-06T14:38:07+00:00

I have created a little test project to try to resolve a problem I

  • 0

I have created a little test project to try to resolve a problem I am having in my main project. I’ve noticed that when retrieving an object from a container the reference count dosen’t increment.

I am confused why this is not the case?

For example this code will not increase the reference count of the hereDoggy object:

//Retrieve the dog, why does this not increment the reference count?
Dog* hereDoggy = [cont1 objectAtIndex:0];

Below is the full example:

-(void)doZombieProblem
{
    NSMutableArray* cont1 = [NSMutableArray array];
    NSMutableArray* cont2 = [NSMutableArray array];
    NSMutableArray* cont3 = nil;


    //Create the dog pointer
    Dog* doggy = [[Dog alloc] initWithName:@"Bernard"];

    //Add to container1
    [cont1 addObject:doggy];

    //Release the dog pointer
    [doggy release];

    while ([cont1 count] > 0)
    {
        //Retrieve the dog, why does this not increment the reference count?
        Dog* hereDoggy = [cont1 objectAtIndex:0];

       //Add it to cont2
        [cont2 addObject:hereDoggy];

        //Remove it from cont1.
        [cont1 removeObjectAtIndex:0];

        //No need to release as we haven't increased the reference count.
        //[hereDoggy release];
    }

    //I should be able to retrieve the dog here from cont2.
    Dog* bernard = [cont2 objectAtIndex:0];

    //No need to release as we haven't increased the reference count.
    //[bernard release];
}
  • 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-06-06T14:38:08+00:00Added an answer on June 6, 2026 at 2:38 pm

    It’s your responsibility as the user of the object to manage it’s retain count. This is because only you, the consumer, know when you are done with it. That’s why just calling [cont1 objectAtIndex:0] doesn’t increment it. NSArray has no clue what you have planned with the object it returns.

    Think of retain count to indicate the number of things owning something. When it’s 0, no one owns it, so let it be garbage collected. If it’s 1, then only 1 thing needs it/owns it (and on up).

    When you call [cont1 addObject:doggy] NSMutableArray will absolutely increment the retain count on it (behind the scenes), just like when you call [cont1 removeObjectAtIndex:0] NSMutableArray will decrement the retain count on it.

    Now, if you need hereDoggy for any period of time, just call retain on it yourself, and then release where appropriate.

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

Sidebar

Related Questions

I am facing another problem in my little test-webapp. I have an EJB module
I am confused over a little problem i have with jquery in my project.
I have created a little example,please have a look, http://jsfiddle.net/bWTwL/ I want to have
Hi I have created a little application to move some files around and put
Ok, I have created a little utility with AppleScript and used Automator to turn
I am a little stuck. I have created almost everything in the picture below.
I have a page which contains a jQuery-UI horizontal slider, created using a little
I'm a jQuery newbie, and I have trouble with a little script I created.
I have written a little console application that uses s#arp. I can create an
I have a problem with encoding. A friend gave me his php/mysql project to

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.