Return [newPossession autorelease]


#1

I have two questions, please:

  1) what is the meaning of "scope"?  Context:  "The pointer to the Possession instance is lost when the scope of randomPossession runs out, but the object still has a retain count of 1."

    Do you just mean when the randomPossession method competes?

  2) for the class method randomPossession, I think I understand the concept of autorelease from your description.  The method created the instance, it needs to get rid of it AFTER it is added to the NSMutableArray.  So after you described the issue with multiple ownership, I expected the line of code

return newPossession;      

// to be followed by the line
[newPossession autorelease];

Instead, you have substituted the code return [newPossession autorelease];

In “main”, the NSMutableArray needs the randomly generated new Possession. So you want to return the newPossession from the method. The way you have written it, you are returning the results of the autorelease function. To me the result would be “waiting to go to instance heaven”! What will the array you are generating do with that??

In essence, what I am asking is: what is returned to the addObject method from “return [newPossession autorelease]” ? I fear my question will be gibberish to you, but I’ll risk it.

Thanks!!

Joel


#2
  1. Yes, when the randomPossession method completes.
  2. When you send autorelease to an object, that object is the return value. So, [a autorelease] returns a. Therefore, the following two lines of code are identical:
[a autorelease];
return a;

-- and --

return [a autorelease];

The latter is just more concise.

In your example of,

return newPossession;
[newPossession autorelease];

the second line of code will never get called. A return means a method ends, no lines of code after a return will be executed.

Keep in mind the difference between autorelease and release. Release tells an object to decrement its retain count. Autorelease puts an object in a pool, some time in the future, that pool will send release to all of the objects in it. The reason we use autorelease is because sometimes we would like to create an object but not be an owner of it. This example shows that: randomPossession does not want to be an owner of the new Possession object, but it wants to return it to some object who may become the owner of it.

It is possible that an autoreleased object returned from a method gets retained by another object, in which case, the retaining object becomes the sole owner of the previously autoreleased object.

It is also possible that an autoreleased object returned from a method does not get retained by another object, in which case, that object gets destroyed.


#3

Beautiful. I didn’t know that [a autorelease] returns a. THANK YOU.


#4

Sorry, upon re-reading the chapter, it was clearly explained. It just took me a while to absorb it all and integrate it. Onto “Delegation”!


#5

Just to clarify, the autorelease message can be in various spots, without any difference right? For example:

in the beginning:
[[[a alloc] init] autorelease]

in the middle:
[a autorelease];
return a;

in the end:

return [a autorelease];

It’s like a “tag for later” approach so that it can be drained by the pool if I’m understanding it correctly.


#6

Correct. In fact, I actually had those three examples in the book but it was taking up too much space so we left it out.