Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
 MacRumors Forums Can someone explain to me exactly what is going on here?

 Jul 9, 2009, 01:56 AM #1 chrono1081 macrumors 604     Join Date: Jan 2008 Location: Isla Nublar Can someone explain to me exactly what is going on here? I wanted to check the apple documentation but apples site doesn't play well with my work proxy : / I'm coming from C++ and learning Objective-C and I was working through an exercise in a book and I realize that a line in the exercise doesn't do what I thought it did. Here is the program: (I'll just write the important parts) First here is part of the fraction class implementation file, notice it creates a memory leak. Code: ```-(Fraction *) add: (Fraction *) f { Fraction *result = [[Fraction alloc] init]; //other stuff here return result; }``` Now here is part of main. Code: ```//At the top of main we have this here creating a new fraction object Fraction *aFraction = [[Fraction alloc] init]; Fraction *sum = [[Fraction alloc] init], *sum2; //stuff here, skipping down to the for loop for (i = 1; i <= n; ++i) { [aFraction setTo: 1 over: pow2]; sum2 = [sum add: aFraction]; [sum release]; //Here it is.... sum = sum2;``` I thought that when I released it I would be destroying the object but right underneath the release of sum it gets used again without being reallocated. Am all I doing is clearing the data stored in sum? Would I need to use some type of dealloc keyword to destroy the object? __________________ Mac Pro (2010): 3.33Ghz Intel Xeon (6 core) - 24 GB RAM - NVidia Quadro k5000 Macbook Air (2010): 2.13 Ghz Intel Core 2 Duo - 4GB RAM 0
Jul 9, 2009, 02:32 AM   #2
ChOas
macrumors regular

Join Date: Nov 2006
Location: The Netherlands
Quote:
 Originally Posted by chrono1081 First here is part of the fraction class implementation file, notice it creates a memory leak.
Not really... You return a pointer to the allocated memory, so you are free to release the memory later. It is not a real leak. (you didn't lose the pointer to the memory)

Quote:
 Code: ```//At the top of main we have this here creating a new fraction object Fraction *aFraction = [[Fraction alloc] init]; Fraction *sum = [[Fraction alloc] init], *sum2; //stuff here, skipping down to the for loop for (i = 1; i <= n; ++i) { [aFraction setTo: 1 over: pow2]; sum2 = [sum add: aFraction]; [sum release]; //Here it is.... sum = sum2;``` I thought that when I released it I would be destroying the object but right underneath the release of sum it gets used again without being reallocated. Am all I doing is clearing the data stored in sum? Would I need to use some type of dealloc keyword to destroy the object?
When you alloc an object it has a retain count of 1 ... You can increase or decrease this retain count by using 'retain' or 'release'. Now, if your object receives a release when it has a retain count of 1 then it is assumed no other code is interested in the object, so the memory for the object can be de-allocated.

That is what is happening here.

1: sum2 is just a pointer to a fraction.
2: add: returns a pointer to an allocated fraction.
3: sum2 now points to the allocated fraction having a retain count of 1.
4: You release sum (retain now 0), thus deallocating the memory originally used by sum, leaving you with a usable/free pointer which you can point to any fraction again.
5: You let sum point to the same fraction as sum2 is pointing to.
0
 Jul 9, 2009, 07:24 AM #3 chrono1081 Thread Starter macrumors 604     Join Date: Jan 2008 Location: Isla Nublar Thank you for the clarification It didn't hit me looking at the exercise that the pointer was never lost. It all makes sense now __________________ Mac Pro (2010): 3.33Ghz Intel Xeon (6 core) - 24 GB RAM - NVidia Quadro k5000 Macbook Air (2010): 2.13 Ghz Intel Core 2 Duo - 4GB RAM 0

 MacRumors Forums