Junior — Middle
How is deferred object release managed in ARC in automatic memory management?
sobes.tech AI
Answer from AI
In ARC (Automatic Reference Counting) in iOS, deferred object release occurs through the autorelease pool mechanism. When an object is marked as autoreleased, it is not released immediately but placed into a special pool, which is cleared later — usually at the end of the current event cycle.
This allows the object to remain alive temporarily, even if its reference count drops to zero, which is convenient for passing objects between methods without immediate release.
Example:
- (NSString *)formattedString {
NSString *temp = [[NSString alloc] initWithFormat:@"Hello, %@", name];
return [temp autorelease]; // object will be released later
}
In modern versions of Objective-C with ARC, programmers typically do not manually manage autorelease, but under the hood, the system uses this mechanism for deferred release to avoid issues with object lifecycle.