1

I have a general idea on how NSAutorelease pool works. we have objects in it which are autoreleased and when the drain method is called. the pool is checked for objects with retaincount as +1, and are thus deallocated.

but what I am not sure of is. We create object of NSAutoRelease pool in the main thread as well as one for each thread. How is a thread related to that particular pool. What happens if we create two or more autorelease pools in one thread.

we just create the pool object and drain it when our work is done. its not like that we get a singleton or something .

Then how does the thread get to that particular pool ?

Explanation of the scenario of what I mean by retain count 1. [slightly incorrect, read the edit]

  • Obj A has a method createAndReturn.
  • createAndReturn creates an object autorel_obj and returns it.

Now it can't just release it as it has to return it. So it will autorelease it and return.

Thus autorel_obj will be in autorelease pool. now say objB calls createAndReturn of ObjA.

and gets hold of autorel_obj and retains it otherwise autorelease pool will drain it. Now when it is being retained by objB, its retain-count is 2.

[Here is the incorrect part corrected in, the EDIT]

Autorelease pool just cant release autorel_obj until it is also being used by objB also. thats why until objB also releases it and its retain count becomes '1', it cant be released.

so By retainCount 1, what I mean is that the object which sent it to the pool is the only one owning it.

and regarding pool and thread relation, Firoze Lafeer answer was helpful.

EDIT to retain count 1 scenario: As correctly pointed out by Firoze, My earlier explanation of retain count 1 needs a change.

autorel_obj will only be released when pool is drained and hence its retain count will go down by 1. It wont be deallocated from memory. Once every other owner obj of autorel_obj releases it and its retain count becomes 0. then only it is deallocated from the memory.

Sorry for all the trouble, Thanks to Firoze for the correction.

1
  • In short; autorelease pools and threads are entirely orthogonal; unrelated.
    – bbum
    Jan 2, 2012 at 18:20

1 Answer 1

3

the pool is checked for objects with retaincount as +1, and are thus deallocated.

I'm not sure I understand that statement completely, but it sounds incorrect to me. There is nothing conditional about autorelease. If you autorelease an object, it will be released when the pool is drained, regardless of its retain count at that point (even if the object had already been deallocated!) It's better to think of "autorelease" as "deferred release".

As for the other question, each thread maintains its own stack of autorelease pools. Each pool is associated with one (and only one) thread.

Which thread is a given pool associated with? The answer is whichever thread created the pool. If you create a new pool where one exists already, then the new pool is "nested" within the existing pool. Objects autoreleased within the scope of that new pool will be released when that pool is drained (when the scope of that pool ends).

I hope that helps?

EDIT

To address your edit:

Your explanation is not correct. The autorelease pool can and does release the object as soon as it is drained. It doesn't wait for objB to release it first. It doesn't even know what other objects may have retained autorel_obj from your example. I think you're confusing release with deallocation.

So the scenario is this:

  • createAndReturn allocates and autoreleases autorel_obj (retain count is +1)
  • objB retains autorel_obj (retain count +2)
  • pool is drained, autorel_obj is released by the pool (retain count +1)
  • at some point in the future, objB releases autorel_obj (retain count 0)
  • autorel_obj is deallocated

So, again, the pool doesn't know and doesn't care what other objects may have retained the object it is releasing. It does the release, unconditionally, when drained. That may not cause the object to be deallocated immediately, but that's not the pool's concern.

4
  • Thanks that definitely helps . Jan 2, 2012 at 7:20
  • Now check. I was little late. I tried editing my comment and give answer here only but couldnt as there is a char limit in comments. Jan 2, 2012 at 7:59
  • 1
    Got it. I would say your explanation is still not correct. I edited my answer to include that. Jan 2, 2012 at 8:07
  • Ohh.. thanks for the clarification.. i really confused deallocation and release. so Autorelease pool just releases it and just the retain count goes down ? which means obj is still in the memory. and when other objects release it and retain count goes down to 0. then it finally it is removed from memory and its dealloc is called. Thanks a lot .. for removing the confusion ..lemme put another edit..to it now. Jan 2, 2012 at 9:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.