Possible Duplicate:
does code in finally get run after a return in objective-c?
Consider this block of Objective C pseudocode:
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
@try {
throw [[NSException alloc] init];
}
@catch (NSException *e) {
throw e;
}
@finally {
[pool drain];
}
Will the pool be drained? Or does the throw in the @catch block render that code unreachable? I feel the pool should be drained but I can’t find documentation on it one way or another.
Yes I could write some code and test it out, but at the moment that isn’t feasible.
Thanks
Yes (docs).
See the very bottom of the linked doc page for a further note on memory management in this case. You’re “OK” in your example because the exception is not itself autoreleased as part of the pool you’re draining in your finally block. But you might leak that exception if no one releases it.
(But there seems to be some ambiguity around exception lifecycle in some cases, see: What is the lifecycle of an object caught in a @catch block?)