Results 1 to 3 of 3
  1. #1

    Member Since
    Jun 12, 2009
    Question 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];

    [f noSuchMethod];

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

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


  2. #2

    Member Since
    May 07, 2009
    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.

  3. #3

    xstep's Avatar
    Member Since
    Jun 25, 2005
    On the road
    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];.
    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.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Icons dragging but no releasing
    By fazza8 in forum macOS - Operating System
    Replies: 2
    Last Post: 06-04-2013, 06:41 AM
  2. MacBook shuts down when releasing power button
    By Astone90 in forum Apple Notebooks
    Replies: 1
    Last Post: 06-24-2012, 07:34 PM
  3. Trouble releasing cd from mac pro
    By pbjorge in forum Other Hardware and Peripherals
    Replies: 2
    Last Post: 12-17-2011, 05:05 PM
  4. reply statement in Mail
    By bmagal in forum macOS - Apps and Games
    Replies: 0
    Last Post: 02-08-2009, 01:46 AM
  5. question about objective C memory allocation
    By thuffir in forum macOS - Development and Darwin
    Replies: 3
    Last Post: 11-18-2008, 10:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts