- (void)foo
{
@try {
for (id o in os){
@autoreleasepool {
for (id o2 in moreOs){
// create a lot of autoreleased objects.
}
//exception raised
}
}
}
@catch (NSException *exception) {
// handle the exception
}
}
-
Will the compiler rewrite the above code to drain the pool in the event of an exception or will it leak?
-
If the compiler does rewrite it, how will it do it?
To address your question, no those objects created in that pool will effectively be leaked until some autoreleasepool further down the stack is drained. There may be no pool further down the stack except the main autoreleasepool for your app which may not get drained for some time.
Try the following to force the drain to happen when this method returns if an exception is caught.
According to documentation, when the outermost autoreleasepool is drained it will drain any nested ones as well.
From the “Scope of Autorelease Pools and Implications of Nested Autorelease Pools” section in Advanced Memory Management Programming Guide