PDA

View Full Version : Can someone explain to me exactly what is going on here?

chrono1081
Jul 9, 2009, 02:56 AM
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.

-(Fraction *) add: (Fraction *) f
{
Fraction *result = [[Fraction alloc] init];

//other stuff here

return result;
}

Now here is part of main.

//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];

[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
Jul 9, 2009, 03:32 AM
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)

//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];

[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
Jul 9, 2009, 08:24 AM
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 :)