If <some code> throws an exception won't that mean that <pool> is
leaked? (and all of its contents up to that point too; applies to
both of our code examples).
Yup. You can work around that by using @finally:
for (i = 0; i < count; i++)
{
NSAutoreleasePool* pool = [NSAutoreleasePool new];
@try{
NSImage* tempSource = [[[NSImage alloc]
initWithContentsOfFile:sPath[i]] autorelease];
// autorelease tempSource to ensure that we get cleaned up
if anything in 'some code' throws an exception.
// pool will automagically be released later if an
exception does happen and [pool release] get bypassed.
// some code
}@finally{
[pool drain];
}
}
This is wrong.
You mustn't drain or release autorelease pools in finally or catch
blocks because exception objects themselves are autoreleased.
It gets sorted out when a higher level autorelease pool is released.