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.
Now here is part of main.
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?
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?