New To Mac-Forums?

Welcome to our community! Join the discussion today by registering your FREE account. If you have any problems with the registration process, please contact us!

Get your questions answered by community gurus Advice and insight from world-class Apple enthusiasts Exclusive access to members-only contests, giveaways and deals

Join today!

 
Start a Discussion
 

Mac-Forums Brief

Subscribe to Mac-Forums Brief to receive special offers from Mac-Forums partners and sponsors

Join the conversation RSS
OS X - Development and Darwin Discussion and questions about development for Mac OS X.

Releasing memory try-catch block statement in Objective-C


Post Reply New Thread Subscribe

 
Thread Tools
KinAZ

 
Member Since: Jun 12, 2009
Posts: 1
KinAZ is on a distinguished road

KinAZ is offline
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;

}
QUOTE Thanks
knobd

 
Member Since: May 07, 2009
Posts: 11
knobd is on a distinguished road

knobd is offline
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.
QUOTE Thanks
xstep

 
xstep's Avatar
 
Member Since: Jun 25, 2005
Location: On the road
Posts: 3,231
xstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to all
Mac Specs: 2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid

xstep is offline
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];.

CameraTime - Time lapse photography for novice and advanced users.

When asking questions, post the version of your software. You'll receive better answers.

Please post your results to the thread as it is good feedback.
QUOTE Thanks

Post Reply New Thread Subscribe


« Backward compatibility of Darwin libraries | Memory management question »
Thread Tools

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread
Thread Starter
Forum
Replies
Last Post
Viewing Photos Doug in Alaska Images, Graphic Design, and Digital Photography 8 03-22-2009 04:29 PM
I am a memory idiot, please help jaster Apple Notebooks 5 12-29-2004 02:00 PM
Trans Intl. offers 1GB iBook G4 memory modules Murlyn Apple Notebooks 0 11-12-2003 01:41 AM

All times are GMT -4. The time now is 06:52 AM.

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

Welcome to Mac-Forums.com

Create your username to jump into the discussion!

New members like you have made this community the ultimate source for your Mac since 2003!


(4 digit year)

Already a member?