Releasing memory try-catch block statement in Objective-C

Joined
Jun 12, 2009
Messages
1
Reaction score
0
Points
1
Hi All!

I'm a newbie to programming. Please look at my code below. Why isn't there a release like [exception release] for the NSException object created? How will the compiler know how to release the NSException object? I understand why/how f is released (from [f release] statement).

++++++++++++++++++++++++++++++++++++++++++++

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Fraction * f = [[Fraction alloc] init];

@try{
[f noSuchMethod];
}

@catch (NSException *exception){
NSLog (@"Caught %@%@", [exception name], [exception reason]);
}

NSLog (@"Execution continues!");
[f release];
[pool drains];
return 0;

}
 
Joined
May 7, 2009
Messages
11
Reaction score
0
Points
1
I am not a newbie to programming but am a newbie to Objective-C. I believe that the AutoReleasePool will take care of the NSException object and that you only have to manually release those objects you create yourself (like the f Fraction object).

If I am incorrect on this, someone please correct me as I am still learning this stuff too.
 
Joined
Jun 25, 2005
Messages
3,231
Reaction score
112
Points
63
Location
On the road
Your Mac's Specs
2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid
I think you want to read "Exception Handling and Memory Management" in Apple's Handling Exceptions document.

Almost all NSException objects (and other types of exception objects) are created autoreleased, which assigns them to the nearest (in scope) autorelease pool.

From the little you've posted, I have to wonder if your try block is contrived. Since you could just test the object for the method (selector), via the respondsToSelector: method.

Oh. The docs say it should be [pool drain]; not [pool drains];.
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top