1
votes

I'm still not 100% with ARC internals.

A few examples that I would like clarified:

What happens with someObjectToReturn = [[SomeClass alloc] init]; that is allocated inside a method and returned? If someObjectToReturnis assigned to e.g. someObject, when will someObjectToReturn be released? When someObject is nilled or does someObjectToReturn still need to be added to an autorelease pool?

Another case is if an array of objects is nilled, are all the objects released?

Lastly, where is a good place to release a copied block object? Or will the block still be released after execution? If the copied block is added to an autorelease pool, when would one drain that pool? Lets say there are 10 blocks, each added to an autorelease pool, draining it too soon would result in the executing blocks to die. Can a copied block be nilled from within itself, such as dispatching the nilling of the block back to the main thread?

1

1 Answers

1
votes

You seem to be overcomplicating things. Under ARC, you can't release objects, or drain autorelease pools, so there is no "right" time to do this. Don't think about retain and release and autorelease (except in tight loops), think about object ownership.

Your specific questions:

  1. The object will be released when the variable that the return value is assigned to goes out of scope or is set to nil. You don't have to worry about it.

  2. Yes, all the contents of an array are released when the array is set to nil, you don't have to worry about it

  3. You can set the value holding the copied block to nil when you're done, or it will disappear when the object owning the copied block goes. You can't drain an autorelease pool under ARC, you can wrap statements in an @autoreleasepool{} block, but this just captures everything in the block - you don't "add" things to the pool or "drain" the pool.