Segmentation Fault Error

Discussion in 'Mac Programming' started by TechnoEagle, Feb 5, 2010.

  1. TechnoEagle macrumors newbie

    Joined:
    Jan 30, 2010
    #1
    I am writing a program in Xcode, but when I run it, I get a "Segmentation fault" error. Here is the method that keeps giving me the error. I don't have much experience with memory management or Objective-C, so help would be appreciated! :D
    Code:
    -(NSString *)description
    {
    	NSString *description=[[NSString alloc] init];
    	description=[NSString stringWithFormat:@"Hello! I am a %1.2f-sided (aka a %@) with angles of %1.2f (%1.2f radians)", numberOfSides, [self getName], [self getAngleInDegrees], [self getAngleInRadians]];
    	[description autorelease];
    	return description;
    }
    Thanks! :apple:
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    Post all of your code, not just where you think things are going wrong. We have no idea what sorts of things numberOfSides, [self getName], [self getAngleInDegrees], and [self getAngleInRadians] are. It seems pretty unusual to me that a polygon could have a non-integer number of sides, but who knows without the rest of your code.

    -Lee
     
  3. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #3
    Try to explain exactly what you think the line "description = [NSString stringWithFormat ...] does.
     
  4. TechnoEagle thread starter macrumors newbie

    Joined:
    Jan 30, 2010
    #4
    I actually just got it to work. :D
    I think the problem was that I used %1.2f where I should have been using %i.
     
  5. Mac Player macrumors regular

    Joined:
    Jan 19, 2006
    #5
    Most of the code you have there is useless.

    1. you are leaking objects since the initially allocated description is never released.
    2. stringWithFormat already return an autoreleased String

    so here is the fixed method

    Code:
    -(NSString *)description
    {
    	return [NSString stringWithFormat:@"Hello! I am a %1.2f-sided (aka a %@) with angles of %1.2f (%1.2f radians)", numberOfSides, [self getName], [self getAngleInDegrees], [self getAngleInRadians]];
    }
    
     
  6. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #6
    You sure it wasn't that you autoreleased an autoreleased object and then returned that object. presumably to use later thus dealloc'ing that object at an inopportune time? (EVEN IF YOU RETAINED IT IMMEDIATELY)

    YOU SURE?!?

    Cause I don't buy that %0.3f vs %i or something caused a segfault.

    I don't buy it in the least...



    IN ADDITON
    Generally if you're going to return %0.2 angles in degrees you should return something like %0.4 angle in radians to keep a similar level of precision.

    1 degree = 0.0174532925 radians, so .01 degree (the precision in degrees) = 0.000174532925 radians (which in your 2 decimal precision is 0.0, not 0.0002)
     

Share This Page