These two Objective-C functions crash with no message
I'm building a header with a bunch of functions to do some astronomy math and so far so good, except these two functions crash the debugger with a Debugging terminated.
message and nothing else. The NSLog
statements print to the console just fine with the correct answers, but they crash. Any ideas?
float calcTimeJulianCent(float julianDate) {
NSLog(@" -calcTimeJulianCent");
float tCentury = (julianDate - 2451545.0) / 36525.0;
NSLog(@" -calculation complete, tCentury = %1.4f", tCentury);
return tCentury;
}
float calcJDFromJulianCent(float tCentury) {
NSLog(@" -calcJDFromJulianCent");
float julianDate = tCentury * 36525.0 +开发者_运维知识库 2451545.0;
NSLog(@" -calculation complete, tCentury = %1.4f", julianDate);
return julianDate;
}
And here's the method that calls the functions:
- (IBAction)doMath {
NSLog(@"-%@:%s called", [self class], _cmd);
// other calls that work
float julianCentury = calcTimeJulianCent(julianDay);
NSLog(@" -calcTimeJulianCent called: %@", julianCentury);
float backToJulianDate = calcJDFromJulianCent(0.1092);
NSLog(@" -calcJDFromJulianCent called: %@", backToJulianDate);
// more calls that work
}
EDIT: Answer - silly mistake. The functions return floats
and I used %@
in the NSLog which is for strings. You'd think Xcode could do more than just crash over that.
I saw your edit. The reason is crashes is because NSLog
expects to find a pointer to an object -- that is, a memory location -- when it finds the %@
format specifier. Obviously a floating-point number cannot be a valid memory address, so the program crashes. Xcode doesn't fix it because NSLog
takes a string and a variable list of arguments. However, in C, you cannot specify types for varargs, so there's no way to tell what type of arguments NSLog
should take, in the general case.
(gcc does provide some format-specifier checking when you call printf
, but the same doesn't happen with NSLog
.)
精彩评论