segmentation fault

Discussion in 'Mac Programming' started by jamesapp, May 12, 2008.

  1. jamesapp macrumors 6502a

    Joined:
    Mar 7, 2008
    #1
    james-collinss-macbook-pro:prog17 jamescollins$ ./prog11.1a
    1/3 + 2/5 = 11/15
    1/3 - 2/5 = -1/15
    1/3 * 2/5 = 2/15
    1/3 / 2/5 = 5/6
    Segmentation fault

    got the above output when i ran a program from a book on Objective-C by Stephen G. Kochan, the program compiled without any error or warning messages.

    here is my test program which i called prog11.1.m

    Code:
    #import "Fraction.h"
    @interface Fraction (MathOps)
    -(Fraction *) add: (Fraction *) f;
    -(Fraction *) mul: (Fraction *) f;
    -(Fraction *) sub: (Fraction *) f;
    -(Fraction *) div: (Fraction *) f;
    @end
    
    @implementation Fraction (MathOps);
    -(Fraction *) add: (Fraction *) f
    {
      // To add two fractions:
      // a/b + c/d = ((a*d) + (b * c)) / (b * d)
      
      Fraction *result = [[Fraction alloc] init];
      int  resultNum, resultDenom;
      resultNum = (numerator * [f denominator]) +
        (denominator * [f numerator]);
      resultDenom = denominator * [f denominator];
      
      [result setTo: resultNum over: resultDenom];
      [result reduce];
      
      return [result autorelease];
    }
    
    -(Fraction *) sub: (Fraction *) f
    {
      // To sub two fractions:
      // a/b - c/d = ((a*d) - (b*c)) / (b * d)
      
      Fraction *result = [[Fraction alloc] init];
      int  resultNum, resultDenom;
      
      resultNum = (numerator * [f denominator])-
        (denominator * [f numerator]);
        resultDenom = denominator * [f denominator];
        
        [result setTo: resultNum over: resultDenom];
        [result reduce];
        
        
        return [result autorelease];
    }
    
    -(Fraction *) mul: (Fraction *) f
    {
      Fraction *result = [[Fraction alloc] init];
      
      [result setTo: numerator * [f numerator]
           over: denominator * [f denominator]];
      [result reduce];
      
      return [result autorelease];
    }
    
    -(Fraction *) div: (Fraction *) f
    {
      Fraction *result = [[Fraction alloc] init];
      
      [result setTo: numerator * [f denominator]
                over: denominator * [f numerator]];
      [result reduce];
      
      return [result autorelease];
    }
    @end
    int main (int argc, char *argv[])
    {
      NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
      Fraction *a = [[Fraction alloc] init];
      Fraction *b = [[Fraction alloc] init];
      Fraction *result;
      
      [a setTo: 1 over: 3];
      [b setTo: 2 over: 5];
      
      [a print]; printf (" + "); [b print]; printf (" = ");
      result = [a add: b];
      [result print];
      printf ("\n");
      [result release];
      
      [a print]; printf (" - "); [b print]; printf (" = ");
      result = [a sub: b];
      [result print];
      printf ("\n");
      [result release];
      
      [a print]; printf (" * "); [b print]; printf (" = ");
      result = [a mul: b];
      [result print];
      printf ("\n");
      [result release];
      
      [a print]; printf (" / "); [b print]; printf (" = ");
      result = [a div: b];
      [result print];
      printf ("\n");
      [result release];
      [a release];
      [b release];
      
      
      [pool release];
      return 0;
    }
    
    one thing that i would like to mention is that the program compiled and ran ok with
    the following line emitted
    Code:
    [pool release];
    
    but when i added the above line i got a segmentation fault when i went to run the program, in the Kochan book a lot of the programs that i looked at in the later chapters like chapter 17 had pool release added?
    do i need this line?
    any help would be appreciated.
     
  2. Nutter macrumors 6502

    Joined:
    Mar 31, 2005
    Location:
    London, England
    #2
    You're over-releasing your result objects.

    Code:
    result = [a sub: b];
    [result print];
    printf ("\n");
    [result release];
    
    The method -sub: returns an autoreleased object, as all methods without "init" or "copy" in the name should. You shouldn't be releasing the result manually here, as it will be released later when you release the autorelease pool.
     
  3. yeroen macrumors 6502a

    yeroen

    Joined:
    Mar 8, 2007
    Location:
    Cambridge, MA
    #3
    Or even releasing anything at all, since this is a short running 1-pass program and all dynamically allocated memory will be reclaimed once the process exits.

    Of course, the objection could be sustained that it's bad form not to explicitly deallocate memory.
     
  4. jamesapp thread starter macrumors 6502a

    Joined:
    Mar 7, 2008

Share This Page