Auto-Release Pools

Auto-Release Pools are a concept that is (mostly) used behind the scenes by Automatic Reference Counting to determine when objects can be released. Auto-release pools are created by the Cocoa runtime and live on the current execution stack. When object references are autoreleased (instead of fully released), rather than decrementing the object's reference count, the object is placed in the currently active auto-release pool. When the Auto-release pool later gets released (usually higher up in the call stack), the object references in the pool will be released at that time.

In most cases, you will not need to create auto-release pools manually, and the system will create them for you as needed. For example, in code written to react to a UI event, you can assume that Cocoa created an auto-release pool before calling your handler, and that objects you allocate within the handler that end up being auto-released will be released after your handler returns.

However, sometimes it is necessary to manually create an auto-release pool in code – for example when doing memory allocations in a long-running loop. All four Elements languages provide a syntax for this, with Oxygene and C# reusing the using keyword syntax:

using autoreleasepool do begin
  // do work here
end;
// the auto-release pool will be cleared here
using (__autoreleasepool)
{
  // do work here
}
// the auto-release pool will be cleared here
autoreleasepool {
  // do work here
}
// the auto-release pool will be cleared here

See Also

Read More

Read more about Auto-Release Pools at these external links: