question about releasing objects created using instance method

Discussion in 'Mac Programming' started by klaxamazoo, Jul 26, 2010.

  1. klaxamazoo macrumors 6502

    Joined:
    Sep 8, 2006
    #1
    Hi,

    I'm going through Kochran's "Programming in Objective-C 2.0" and I have a question about releasing objects allocated from an object's instance method as shown in Program 7.6 on page 153-154.

    I'm trying to figure out how sum2 is released where only sum is explicitly released in the for loop and at the end of the main function.

    I notice that sum is released just before the sum = sum2 statement and I was wondering exactly what the = sign does. I thought that it set the instance variables in the sum object equal to those in the sum2 object.

    Clearly I'm wrong but I would like some help understanding exactly what occurs when I write sum = sum2.

    Here is the Main Code
    Code:
    #import "Fraction.h"
    
    int main (int argc, const char * argv[]) {
        NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
        Fraction *aFraction = [[Fraction alloc] init];
    	Fraction *sum		= [[Fraction alloc] init];
    	Fraction *sum2;		// sum2 is allocated by "add" method
    	int i, n, pow2;
    	
    	[sum setNumerator: 0 setDenominator: 1]; // initilize sum to 0
    	
    	NSLog(@"Enter your falue for n");
    	scanf ("%i", &n);
    	
    	pow2 = 2;
    	for (i = 1; i <= n; ++i) {
    		[aFraction setNumerator: 1 setDenominator: pow2];
    		sum2 = [sum add: aFraction];
    		[sum release];						// release previous sum
    		
    		sum = sum2;
    		pow2 *= 2;
    	}
    	
    	NSLog(@"After %i iterations, the sum is %g", n, [sum convertToNumber]);
    	[aFraction	release];
    	[sum release];
    	//[sum2 release];
    	
    	
        [pool drain];
        return 0;
    }
     
  2. rdyornot macrumors newbie

    Joined:
    Sep 15, 2007
    #2
    sum is a pointer to a fraction object.
    sum2 = sum assigns sum2 pointer to point at the same object / same place in memory.

    for example if sum and sum2 are both pointers (as they are in your code):
    sum2 = sum;
    sum = 5;
    sum2 will now also = 5.
    sum = 8;
    sum2 will now also = 8 etc...

    does this help? i'm rather new as well so my explanations may not be as clear as they need to be.
     
  3. klaxamazoo thread starter macrumors 6502

    Joined:
    Sep 8, 2006
    #3
    Since sum2 and sum are both pointers to objects and by setting sum2 = sum; they are now pointing at the same object. I assume that means that releasing one releases the other? Will I have any memory leaks because I only release one pointer?

    It looks like I'll have to reread the section on pointers.

    What do I do if I want to copy the values of sum2 to sum?
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    This is wrong.

    One reason it's wrong is because neither 5 nor 8 are Fraction objects.

    Another reason it's wrong is that assigning a pointer to a variable, such as 'sum = 5', has no effect at all on any other variables, such as sum2. So your assertions about sum2 are wrong.

    If you had the same pointer assigned to both variables, and changed the thing pointed at such as by sending a message that changes the object's numerator or denominator, then your assertions would be right. But that's not what you're doing.
     
  5. Catfish_Man macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #5
    Yes. To phrase it a little more accurately, there's only one, and releasing it via either pointer releases it.

    Looks ok to me.

    http://boredzo.org/pointers/ is a very thorough guide a friend of mine wrote.

    http://developer.apple.com/mac/libr...s/NSCopying_Protocol/Reference/Reference.html

    (Roughly speaking, implement a -copy method on Fraction that allocates a new Fraction and sets it values to be equal to the original's.
    More accurately, implement -copyWithZone: and ignore the zone argument. -copy will call -copyWithZone: )
     
  6. klaxamazoo thread starter macrumors 6502

    Joined:
    Sep 8, 2006
    #6
    Thanks a lot for everybody's help. I'm in grad school and am trying to transition from the Matlab and Fortan to a more flexible programming language. It has been a lot of fun so far and Kochran's book really helps a lot.

     

Share This Page