views:

240

answers:

3

Sorry, this may be a noob question but I'm working with CoreLocation and this has be stumped.

I'm looking up the currentLocation using a singleton that was recommended on this site and when I get the currentLocation object, it returns true to a not nil check. However, when I try to print out its description, it throws EXC_BAD_ACCESS.

//WORKS Current location 8.6602e-290
NSLog(@"Current location %g",currLoc);

//DOESN'T WORK
NSLog(@"Current location %@",[currLoc description]);

//DOESN'T WORK - Is this causing the description to fail as well?
NSLog(@"Current location %g",currLoc.coordinate.latitude);

Why am I able to see something on the first one but not the others? BTW, this is being run on a 3.1.2 simulator Thanks.

+1  A: 

currLoc.coordinate.latitude is type double... you can use %f if %g is not working

NSLog(@"Current location %f",currLoc.coordinate.latitude);
mihirpmehta
That also throws an EXC_BAD_ACCESS.
A: 

currLoc might be returning TRUE for not Nil check. But there may be chances of having freed object(Dangling pointer) which will pass the nil check condition. This might be you problem.

Instead of using the currLoc member directly, use Accessor for currLoc. This will fix your problem :)

Manjunath
Sorry, maybe I wasn't clear. currLoc is actually a local variable that I'm setting in this controller but it's set to locationController.currentLocation. LocationController is the interface implementing CLLocationManagerDelegate.
A: 
// malformed: %g format specifier expects type
// double, you're passing an objc object.
// sending arguments through (...) with the
// invalid types/widths is a near certain way
// to crash an app, and the data will be useless
// to the function called (NSLog in this case)
NSLog(@"Current location %g",currLoc);

// try running the app with zombies enabled.
// in short, enabling zombies (NSZombiesEnabled)
// is a debugging facility to determine whether
// you use an objc object which would have been freed.
NSLog(@"Current location %@",[currLoc description]);

// as long as currLoc is a pointer to a valid (non-freed)
// objc object, then this should be fine. if it is not valid
NSLog(@"Current location %g",currLoc.coordinate.latitude);
Justin
I guess my question was in what situations could currLog pass a nil check and still throw an error while trying to access currLoc.coordinate.latitude. I was checking the currLoc to make sure that it was being set to some value by LocationManager and I was able to get around the problem by add some BOOL var whose sole purpose is to track the setting of that variable. Thanks for the input. ps. I usually have NSZombie enabled while doing any testing and I didn't see anything helpful while trying to debug this. T