Results 1 to 4 of 4
  1. #1
    Understanding "release"

    Member Since
    Jul 18, 2010
    Posts
    1
    Understanding "release"
    I've bolded the part of the code I don't fully understand.

    • Won't sum = sum2 just overwrite the old sum? So why do you need to release it beforehand?
    • Why isn't sum2 released at the end of the program? Should it be?


    Code:
    #import “Fraction.h”
    
    int main (int argc, char *argv[])
    {
    
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    Fraction *aFraction = [[Fraction alloc] init]; Fraction *sum = [[Fraction alloc] init], *sum2; int i, n, pow2;
    
    [sum setTo: 0 over: 1]; // set 1st fraction to 0
    
    NSLog (@”Enter your value for n:”);
    scanf (“%i”, &n);
    pow2 = 2;
    
    for (i = 1; i <= n; ++i) {
         [aFraction setTo: 1 over: pow2];
         sum2 = [sum add: aFraction];
         [sum release]; // release previous sum
         sum = sum2;
         pow2 *= 2;
    }
    
    NSLog (@”After %i iterations, the sum is %g”, n, [sum convertToNum]); [aFraction release];
    [sum release];
    [pool drain];
    
    return 0;
    }

  2. #2
    Understanding &quot;release&quot;

    Member Since
    Feb 25, 2009
    Posts
    2,109
    Specs:
    Late 2013 rMBP, i7, 750m gpu, OSX versions 10.9.3, 10.10
    You need to be aware of your memory management - especially if you're learning to ultimately program apps for an iPhone/iPod/iPad.

    ANY time you alloc memory, unless you specifically move it into the autorelease pool (or you're on a device and are using garbage collection), you need to release it yourself.

    Let's say in that case, your n was set at 100,000 (God only knows why you'd want to go that high), and you didn't release - you'd have 100,000 objects in memory that you've now lost control of because you assigned sum2 to sum without releasing sum first. Now imagine that each of those items took 1000 bytes, you've used up 100,000,000 bytes of memory (give or take) and if you were on an iPhone, you'd be getting a warning that memory is getting low and you have NO way to release it since it's not in an autorelease pool you can drain.

    All sum is is a pointer to a block of memory.

    When you type:

    sum = sum2

    you've now set sum to point to the block of memory that sum2 is pointing at. Once you've done that you no longer have a reference to the block of memory that sum was pointing at. Since you don't have that reference you can't release that memory.

    It may seem like a minor thing, but it can add up - especially if you have a lot of large blocks of memory, or are on an iPhone where there is no garbage collection and you've very limited memory to work with compared to a full Mac.

    sum2 isn't released at the end because just before falling out of the for loop, sum has been set to sum2 (not the value that sum2 holds, it's now pointing to the same block of memory as sum2) so you only need to release one of the two objects. You could have released either sum or sum2, but not both. Once you've released sum there should be a 0 retain count and you don't need to release again.

    There is a caveat - and I only know that it's working this way because I know you're reading Programming in Objective-C 2.0 - NORMALLY, a method that is called like what you have:

    sum2 = [sum add: aFraction];

    you would be returned an autoreleased object. But, you haven't gotten to the portion of the book that discusses autorelease. At this point - if I'm remembering my chapters right, in that add: method, it's alloc'ing memory but not assigning it to the autorelease pool.
    My Macs: Late 2013 rMBP w/ 750m; Mac mini G4, 1.25 GHz, 512m ram (server); Late 2011 11" MBA, 1.8GHz i7, 4Gig Ram, 256Gig SSD, HD3000; Powerbook 12" G4 1.33GHz running Debian as a server; Apple TV (1080p version)

  3. #3
    Understanding &quot;release&quot;
    xstep's Avatar
    Member Since
    Jun 25, 2005
    Location
    On the road
    Posts
    3,231
    Specs:
    2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid
    You haven't supplied enough code for anyone to have a good response. You'll need to also supply the Fraction source. For instance, what is returned from the add: method? We don't know if it is a fraction type, or a object pointer, or something else.

    I would expect this code to crash if n is greater than 1 because of the release.

    EDIT: I see Nethfel knows the source of the material and was able to give a better response.
    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.

  4. #4
    Understanding &quot;release&quot;

    Member Since
    Feb 25, 2009
    Posts
    2,109
    Specs:
    Late 2013 rMBP, i7, 750m gpu, OSX versions 10.9.3, 10.10
    Yeah, the only reason I knew is because I read the book recently and I recognized the code

    If he's using the code I'm pretty sure he's using - the add: method AT THIS POINT in the book (around chapter 7) is:

    Code:
    -(Fraction *) add: (Fraction *) f
    {
      Fraction *result = [[Fraction alloc] init];
      int resultNum, resultDenom;
    
      resultNum = numerator * f.denominator + denominator * f.numerator;
      resultDenom = denominator * f.denominator;
     
      [result setTo: resultNum over: resultDenom];
      [result reduce];
    
      return result;
    }
    Later in the book when he gets into memory management - at that point your instructed on how to correct the code to return an autorelease object rather then an object that needs to be released by calling code.
    My Macs: Late 2013 rMBP w/ 750m; Mac mini G4, 1.25 GHz, 512m ram (server); Late 2011 11" MBA, 1.8GHz i7, 4Gig Ram, 256Gig SSD, HD3000; Powerbook 12" G4 1.33GHz running Debian as a server; Apple TV (1080p version)

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 1
    Last Post: 02-16-2015, 02:40 AM
  2. New iPhone rumors claim "radical new case design," August release
    By OneMoreThing... in forum Apple Rumors and Reports
    Replies: 10
    Last Post: 06-22-2011, 12:45 AM
  3. Need help understanding "memory swap"..
    By farmcock in forum Switcher Hangout
    Replies: 2
    Last Post: 05-07-2008, 02:23 AM
  4. About Rumored "Brushed Aluminum" iMac Release
    By DavisCollins in forum Apple Rumors and Reports
    Replies: 20
    Last Post: 06-09-2007, 11:58 AM
  5. Got "Worms", "Viruses" and "Bugs" in your puter?
    By hype.it in forum Schweb's Lounge
    Replies: 14
    Last Post: 06-15-2004, 03:18 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
  •