Demonofloom
Demonofloom

Reputation: 240

Why does the reference count when retrieving object from container not increase?

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];
}

Upvotes: 0

Views: 679

Answers (2)

Lorenzo B
Lorenzo B

Reputation: 33428

In this case, if you want to increase the retain count for your object you need to send a retain (or a copy) message.

As a rule of thumb

You need always to balance your retains (or copyies) with your releases. If you don't do it you can have memory leaks. Otherwise switch to the ARC feature to avoid the code amount to write and simplify your life.

Here a useful link to understand how Memory Management works.

MemoryMgmt

I commented your code to understand what is going on:

// the object referenced by doggy has a retain count of 1
Dog* doggy = [[Dog alloc] initWithName:@"Bernard"];

// now the retain count is 2 since you added to a container class like NSArray
[cont1 addObject:doggy];

// now the retain count is 1
[doggy release];

Then, within the while statement:

// the retain count still remains 1
Dog* hereDoggy = [cont1 objectAtIndex:0];

// the retain count increases to 2
[cont2 addObject:hereDoggy];

// the retain count goes to 1
[cont1 removeObjectAtIndex:0];

Since, the object is maintained alive by cont2 you are able to access it.

If you do [cont2 removeObjectAtIndex:0]; the retain count reaches 0 and the object is deallocated automatically.

Upvotes: 4

Andy Obusek
Andy Obusek

Reputation: 12832

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.

Upvotes: 2

Related Questions