RetainedLocalRef

  • @RetainedLocalRef annotation ensures local variables referencing objects from AutoreleasePool containers remain valid even after the container is drained.

  • This annotation is crucial when dealing with scenarios like ThreadPoolExecutor tasks where objects might get deallocated before the local variable goes out of scope.

  • Without this annotation, local variables referencing objects in AutoreleasePool containers might become invalid after the pool is drained, leading to potential issues.

  • By retaining the local reference, the annotation ensures that the object remains accessible and usable even after its container is removed from the AutoreleasePool.

public abstract @interface RetainedLocalRef implements Annotation

Annotation that indicates a local variable should be retained outside of any subsequent AutoreleasePool use. Otherwise, if a local variable has a copy of an object in a container that is removed in an AutoreleasePool, it will be deallocated before the local variable goes out of scope.

For example, a ThreadPoolExecutor is used to process a list of tasks, removing each task from the list as it is processed. ThreadPoolExecutor tasks are run inside of an AutoreleasePool, since these executors are often long-lived. If a local variable is initialized to one of the task list's elements, by default that variable won't be valid (will be deallocated) after task processing. Adding a LocalRetain annotation to the local variable ensures it is still valid after task processing.

Inherited Method Summary