0

I'm having issues porting a Java project I worked on a while ago to Objective-C code - I get "Program received signal: "EXC_BAD_ACCESS"", on the first line in this code:

-(Point3D *) unit {
  NSLog(@"%@%@", "X: ", x);
  double length = [self length];
  return [[Point3D alloc] initWithX:x/length andY:y/length andZ:z/length];
}

called from here:

-(id) initWithStart:(Point3D *)start andDirection:(Point3D *)dir {

  if ( self = [super init] ) {

    NSLog(@"%@%@", @"Direction:", [dir toString]);

    printf("Trying to find unit length of direction...\n");
    NSLog(@"%@", [[dir unit] toString]);


    self.start = start;
    self.direction = [dir unit];
  }

  return self;
}

Console output is:

2011-12-09 17:20:14.021 RayTracerProject[16607:407] Direction:(0,0,20)
Trying to find unit length of direction...

The toString method of the Point3D looks like this:

-(NSString *) toString {
  NSNumber *xstring = [NSNumber numberWithDouble:self.x];
  NSNumber *ystring = [NSNumber numberWithDouble:self.y];
  NSNumber *zstring = [NSNumber numberWithDouble:self.z];

  NSString * str = @"(";
  str = [str stringByAppendingString:[xstring stringValue]];
  str = [str stringByAppendingString:@","];
  str = [str stringByAppendingString:[ystring stringValue]];
  str = [str stringByAppendingString:@","];
  str = [str stringByAppendingString:[zstring stringValue]];
  str = [str stringByAppendingString:@")"];

  return str;
}

So, from what I can see, my (Point3D *) dir is alive and well when I check what the value is using my [dir toString] call. But when I try to call [dir unit], it seems I no longer have the variables I did in the object, hence the EXC_BAD_ACCESS error.

What am I doing wrong here? I think it's something to do with the way I'm managing (or not) my memory usage, but I don't know what it is.

2 Answers 2

2

The NSLog in your method 'unit' should be:

NSLog(@"%@ %g", @"X:", x);

Moving the space is optional, but just makes it clearer. The key issues were that you were missing the @ before the "X: ", and you need to use %g rather than %@ because from your other code x is a double rather than an NSObject. Equivalent—but simpler and thus better—would also be:

NSLog(@"X: %g", x);

Apple's documentation provides the definitive guide to string format specifiers and that and the surrounding documentation explain the use of format specifiers like %@ (to reference an Objective C object) and %g (to reference a 64-bit floating-point number) when constructing an NSString.

Sign up to request clarification or add additional context in comments.

3 Comments

I think our edits crossed in the post. I changed my mind about what the issue was! :)
:) Thanks for the response. I haven't yet found something explaining all the %@, %g etc - but I was bashing my head against this all afternoon - all working now.
I've added a link to the Apple documentation that is the definitive guide on those string format specifiers, which are confusing until you get your head around them. :)
0

It seems this line here:

NSLog(@"%@%@", "X: ", x);

should look more like this:

NSLog(@"%@%lf", @"X: ", self.x);

Note a few changes here:

  • From your other code, it seems you have a property named 'x'. self.x references that property. (You may or may not also have an instance variable named 'x')
  • Also from your other code, it seems your property 'x' is a double. So the placeholder for a double is %lf, not %@. %@ is for NSObjects (and descendants)
  • NSString literals start with @. So @"X: " is an NSString literal. What you had is a C string.

EDIT

Well, I'm slow as usual. :) Ignore what I said and accept Duncan's answer, which was faster and now cleaner, actually.

1 Comment

:) Thanks for the response - all afternoon trying to figure it out on my own, and two answers pinpointing my issues within 10 minutes of posting here. Much appreciated!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.