I begin to program in Objective-C after many years of Java development. One issue I'm struggling with a bit is memory management. In particular, most examples in books and on the Internet do not seem to take into account memory leaks due to exceptions. For example, consider the following method:
-(void) doSomething
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
NSString *data = [NSString stringWithString@"Hello"];
[PotentialExeptionThrower maybeThrowException];
[pool drain];
}
Should you rewrite the above to prevent memory leaks?
-(void) doSomething
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
@try {
NSString *data = [NSString stringWithString@"Hello"];
[PotentialExeptionThrower maybeThrowException];
} @finally {
[pool drain];
}
}
Is the code above less efficient due to @ try- @ catch ?
Thank!
source
share