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.
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:
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.