views:

53

answers:

3

I am getting a EXC_BAD_ACCESS signal when calling the following line:

self.distance = [NSNumber numberWithDouble:[currentLocation distanceFromLocation: self.location]];

This is only happening in iOS 3.2 for iPad,

I know this is a memory issue but i can't seem to see what is wrong with the above line?

edit: here is the full method:

-(void)updateDistance:(CLLocation *)currentLocation {

    self.distance = [NSNumber numberWithDouble:[currentLocation distanceFromLocation:self.location]];

    placeWrapper.distance = self.distance;

}

which is called like so:

[place updateDistance:self.currentLocation];

self.currentLocation is created here:

CLLocation *location = [[CLLocation alloc] initWithLatitude:newLocation.coordinate.latitude longitude:newLocation.coordinate.longitude];

self.currentLocation = location;

[location release];

Another edit :)

here is the stack trace: http://pastie.org/1222992

A: 

It's difficult to say without demonstrating where/how you're creating "currentLocation", "location", or possibly even "self". I'm guessing either currentLocation or self.location are not properly retained on creation/setting.

Joshua Nozzi
sorry for poor context, i have updated my question
Alex
How have you declared your -currentLocation property? Did you synthesize the accessors or write them manually?
Joshua Nozzi
it is synthesized
Alex
You forgot to answer my first question. I was referring to your @property ...
Joshua Nozzi
sorry my bad, CLLocation *currentLocation; inside the header interface and then @property (nonatomic, retain) CLLocation *currentLocation;
Alex
stack trace is here if it helps: http://pastie.org/1222992
Alex
A: 

You need to retain something...

[currentLocation retain]

or

[self.location retain];

but you have to do it further up the code. Something's getting "forgotten" or goes "out of scope" so try those retains.

DON'T FORGET TO RELEASE WHATEVER IT IS THAT YOU'RE RETAINING.

Thomas Clayson
+1  A: 

Run your code with NSZombieEnabled set. This should tell you if you are over releasing or under retaining somewhere.

JeremyP
hmm it's not coming up with anything, have tested it is working by over releasing an object on purpose
Alex
Next thing is to run the code in the debugger and get a stack trace to find out what is causing the bad access.
JeremyP
here is the stack trace http://pastie.org/1222992
Alex
No I meant run it in the Xcode debugger, it should break on the EXC_BAD_ACCESS and you can then examine the variables in each stack frame to see which ones look like they are garbage.
JeremyP
I can see from the stack trace, however, that self.location must be a garbage value for some reason.
JeremyP
awesome thanks for your help, i was able to track it back through the debugger to the location being set. I wasn't checking for null values in regards to the lat and lon of the object and therefore the location of the object wasn't setting correctly
Alex