Re: Immediate memory release
Re: Immediate memory release
- Subject: Re: Immediate memory release
- From: "Melissa J. Turner" <email@hidden>
- Date: Wed, 30 Apr 2008 20:35:36 -0700
On Apr 30, 2008, at 19:59, Chris Suter wrote:
On 01/05/2008, at 12:27 PM, Jens Alfke wrote:
On 30 Apr '08, at 5:53 PM, Graham Cox wrote:
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.
This is documented somewhere in the docs.
Chris is correct.
If you really need to drain the pool, you need to
NSAutoreleasePool *pool = [NSAutoreleasePool new];
id exception;
@try {
// insert code here
} @catch (NSException *ex) {
exception = [ex retain];
@throw ex;
} @finally {
[pool drain];
if (exception) {
[exception autorelease];
}
}
(Disclaimer: coded in mail)
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden