Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

chrono1081

macrumors G3
Original poster
Jan 26, 2008
8,451
4,149
Isla Nublar
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?
 

ChOas

macrumors regular
Nov 24, 2006
139
0
The Netherlands
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)

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.
 

chrono1081

macrumors G3
Original poster
Jan 26, 2008
8,451
4,149
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 :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.