Mac Developer Library

Developer

Foundation Framework Reference NSAutoreleasePool Class Reference

Options
Deployment Target:

On This Page

NSAutoreleasePool

The NSAutoreleasePool class is used to support Cocoa’s reference-counted memory management system. An autorelease pool stores objects that are sent a release message when the pool itself is drained. More...

Inheritance


Conforms To


Import Statement


Not Applicable @import Foundation;

Availability


Available in OS X v10.0 and later.
  • Releases and pops the receiver.

    Declaration

    Objective-C

    - (void)release

    Discussion

    In a reference-counted environment, since an autorelease pool cannot be retained (see retain), this method causes the receiver to be deallocated. When an autorelease pool is deallocated, it sends a release message to all its autoreleased objects. If an object is added several times to the same pool, when the pool is deallocated it receives a release message for each time it was added.

    In a garbage-collected environment, this method is a no-op.

    Special Considerations

    You should typically use drain instead of release.

    Import Statement

    See Also

    – drain

  • In a reference-counted environment, releases and pops the receiver; in a garbage-collected environment, triggers garbage collection if the memory allocated since the last collection is greater than the current threshold.

    Declaration

    Objective-C

    - (void)drain

    Discussion

    In a reference-counted environment, this method behaves the same as release. Since an autorelease pool cannot be retained (see retain), this therefore causes the receiver to be deallocated. When an autorelease pool is deallocated, it sends a release message to all its autoreleased objects. If an object is added several times to the same pool, when the pool is deallocated it receives a release message for each time it was added.

    Special Considerations

    In a garbage-collected environment, release is a no-op, so unless you do not want to give the collector a hint it is important to use drain in any code that may be compiled for a garbage-collected environment.

    Import Statement

    Availability

    Available in OS X v10.4 and later.

  • Raises an exception.

    Declaration

    Objective-C

    - (id)autorelease

    Return Value

    self.

    Discussion

    In a reference-counted environment, this method raises an exception.

    Import Statement

  • Raises an exception.

    Declaration

    Objective-C

    - (id)retain

    Return Value

    self.

    Discussion

    In a reference-counted environment, this method raises an exception.

    Import Statement

  • Adds a given object to the active autorelease pool in the current thread.

    Declaration

    Objective-C

    + (void)addObject:(id)object

    Parameters

    object

    The object to add to the active autorelease pool in the current thread.

    Discussion

    The same object may be added several times to the active pool and, when the pool is deallocated, it will receive a release message for each time it was added.

    Normally you don’t invoke this method directly—you send autorelease to object instead.

    Import Statement

    Availability

    Available in OS X v10.0 and later.

    See Also

    – addObject:

  • Adds a given object to the receiver

    Declaration

    Objective-C

    - (void)addObject:(id)object

    Parameters

    object

    The object to add to the receiver.

    Discussion

    The same object may be added several times to the same pool; when the pool is deallocated, the object will receive a release message for each time it was added.

    Normally you don’t invoke this method directly—you send autorelease to object instead.

    Import Statement

    Availability

    Available in OS X v10.0 and later.

    See Also

    + addObject:

  • Displays the state of the current thread's autorelease pool stack to stderr.

    Declaration

    Objective-C

    + (void)showPools

    Special Considerations

    Import Statement

    Availability

    Available in OS X v10.0 and later.