Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

TechnoEagle

macrumors newbie
Original poster
Jan 30, 2010
17
0
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:
 
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
 
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:

Try to explain exactly what you think the line "description = [NSString stringWithFormat ...] does.
 
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.
 
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;
}

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]];
}
 
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)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.