Search code examples
iosobjective-cautoreleasensautoreleasepool

Who calls autorelease pool


Who calls autorelease pool or who manages it. I call autorelease on my variable which is inside a function, but who manages this autorelease call, the calling function, or the caller function or who does?


Solution

  • First of all, if you are saying autorelease, don't. Stop using manual memory management and use ARC. It knows more than you do.

    Okay, so let's say you do say autorelease. Then it is placed in the autorelease pool and its retain count remains incremented. Its retain count will be decremented again when the autorelease pool is drained. When that happens depends on what autorelease pool you're talking about.

    • If you actually made this autorelease pool, then it drains when you tell it to drain. Under ARC, that happens when we come to the end of the @autoreleasepool{} directive block.

    • If it's the default autorelease pool, the runtime takes care of it and you have no knowledge or control over the matter. You can be pretty sure there will be a drain call after all you code finishes and the app is idle, but there's nothing guaranteed about it.