Leakage Method in Target C

I work through Stephen Kochan Programming in Objective-C (which I must admit to being a complete newbie).

My current program is a fraction calculator. I have methods for adding, subtracting, multiplying and dividing. The tools tell me that they all leak (just a tiny bit, but this is a tiny program.)

Here's the definition of the subtraction method (the rest follow a very similar form):

-(Fraction *)   subtract: (Fraction *) f;
{
    Fraction    *result = [[Fraction alloc] init];
    int         resultNum, resultDenom;

    resultNum = numerator * f.denominator - f.numerator * denominator;
    resultDenom = denominator * f.denominator;

    [result setTo: resultNum over: resultDenom];

    return result;
    [result release];
}

Thoughts to plug a leak? Thanks in advance.

In addition, I looked at other explanations on the site, but, unfortunately, I do not think that someone else asked for something so basic.

+3
source share
2 answers

The problem is that in:

return result;
[result release];

-release result, return . .

, :

return [result autorelease];

, -release -autorelease, -release result, . , - - , .

+8

:

return result;
[result release];

return [result autorelease];
+4

All Articles