Question

I need some clarification for following code.

I have a array which contain ClassA object. Then i retrieve one of it's object and assign it to local variable object1.

 ClassA object1 = [ClassAObjectContainer objectAtIndex:0];

( Q.1 - Does this increase retain count?? i think it's not )

Now i remove object

  [ClassAObjectContainer removeObjectAtIndex:0];

( Q.2 - So now object1 point to deallocated instance?? and if i send message to it will it crash??

So i should use ClassA object1 = [[ClassAObjectContainer objectAtIndex:0] retain]; if i want to keep the object alive and use it later.

Another things, which one is better approach when object is returned from a method

- (ClassA *)calle
{
    ClassA *object1 = [[ClassA alloc] init];
    return [object1 autorelease];
}

- (void)Caller
{
    ClassA *object2 = [self calle];
    //now suppose object1 is auto-released, so in this case object2 points to deallocated instance?
}

So shouldn't it be better-

- (void)CallerMethod
{
    ClassA *object2 = [[self calle] retain];
   // Do other works.
   /.....

    [object2 release];
}

ANother things what happend in this case,

- (ClassA *)calle2
    {
        ClassA *object1 = [[ClassA alloc] init];
        return [object1 autorelease];
    }


 - (void)CallerMethod
    {
        ClassA *object2 = [self calle];

        object2 = [self calle2];

    }

Is there any memory leak here, or as ClassA *object2 = [self calle] is weak/assign, there is no problem after 2nd initializations object2 = [self calle2], we can assign 100 objects this way and no memry leak will happen.

Thanks for your time.

Was it helpful?

Solution 2

Q.1 - Does this increase retain count?? i think it's not

Not necessarily, but it might increase it temporarily. The implementation of objectAtIndex: could retain and then autorelease the object before returning it, and that would be a very friendly thing to do in light of...

Q.2 - So now object1 point to deallocated instance?? and if i send message to it will it crash??

It might, if -objectAtIndex: didn't take care to return an autoreleased object as described above. If there's any doubt, you should retain the object yourself before removing it from the container. So, for example, the documentation for -[UIView removeFromSuperView] says:

If you plan to reuse a view, be sure to retain it before calling this method and release it again later as appropriate.

That's exactly because the superview will release the view. If you haven't retained it, it may be deallocated at that point.

So i should use ClassA object1 = [[ClassAObjectContainer objectAtIndex:0] retain]; if i want to keep the object alive and use it later.

Certainly you should retain it if you want to use it "later". If you're going to use it "now" then you may not need to retain the object, depending (again) on what ClassAObjectContainer says it does.

I'm not sure I follow the rest of your question. Generally, though, when you get an object back from some other method, you should be able to use that object without retaining it unless you plan to keep it around beyond the end of the method in which you received it. So, if you're assigning it to an instance variable or something, you should take ownership of the object by calling retain. The behavior of a collection complicates things a little -- if you get an object from a collection and then tell that collection to remove the object, you really should expect to have to retain the object first if you'll need it after it's been removed. On the other hand, if you use ARC (and why wouldn't you?), you don't have to worry about such things.

OTHER TIPS

Here are the answers

Q1: NO . There will be no change in retainCount for Simple assignment.

Q2: It depends on the ratainCount of the object at Index i.e when its removed from array its retainCount will be decremented by 1. If retainCount becomes 0 after removal, it causes a crash otherwise it won't.

AutoRelease: The golden rule while using an autoreleased object returned by method (other than new/copy) is to retain it. Once the work is done you should release it. You can do as many intializations as you want, but it may overload the autorlease pool with all the objects created.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top