The code you posted is correct. Any accident has a different cause.
Since result is a strong link outside the scope of auto-advertising, ARC is responsible for maintaining its performance when exiting the pool, and this happens. You do not have to do anything.
More specifically, ARC generates code equivalent to this:
void *_arp = objc_autoreleasePoolPush(); temp1 = objc_retainAutoreleasedReturnValue([self stringByReplacingOccurrencesOfString:@"&" withString:@"&"]); temp2 = objc_retainAutoreleasedReturnValue([temp1 stringByReplacingOccurrencesOfString:@"\"" withString:@"""]); objc_release(temp1); result = objc_retainAutoreleasedReturnValue([temp2 stringByReplacingOccurrencesOfString:@" " withString:@" "]); objc_release(temp2); // result is not released here objc_autoreleasePoolPop(_arp); return objc_autoreleaseReturnValue(result); // Result is returned autoreleased, or handed off to a matching objc_retainAutoreleasedReturnValue() in the caller.
Note that temporary variables are handled by objc_retainAutoreleasedReturnValue / objc_release . Due to the runtime optimization implemented in objc_retainAutoreleasedReturnValue and objc_retainAutoreleasedReturnValue , this means that temporary values will actually be released immediately, even if they are not put into the autorun pool if -stringByReplacingOccurrencesOfString: built using. I assume that most system frameworks have not yet been created.
source share