Mac Forums

Mac Forums (http://www.mac-forums.com/forums/)
-   OS X - Development and Darwin (http://www.mac-forums.com/forums/os-x-development-darwin/)
-   -   Releasing memory try-catch block statement in Objective-C (http://www.mac-forums.com/forums/os-x-development-darwin/155626-releasing-memory-try-catch-block-statement-objective-c.html)

KinAZ 06-15-2009 05:54 PM

Releasing memory try-catch block statement in Objective-C
 
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;

}

knobd 06-17-2009 03:56 PM

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.

xstep 06-18-2009 12:43 PM

I think you want to read "Exception Handling and Memory Management" in Apple's Handling Exceptions document.

Quote:

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];.


All times are GMT -4. The time now is 08:26 PM.

Powered by vBulletin
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.