EXC_BAD_ACCESS signal received - iphone

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

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

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.

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.

Related

Objective-C crash issue: NSInvalidArgumentException

I am stuck in it for a long time, but can not find a solution. Here is my code:`
NSLog(#"[tempArray retainCount]: %d",[tempArray retainCount]);
tempArray = [[NSMutableArray alloc] initWithArray:[allRemainingProductsDictionary objectForKey:[[allRemainingProductsDictionary allKeys]objectAtIndex:counter]]];
NSMutableDictionary *tempDictionary = [[NSMutableDictionary alloc] init];
[tempDictionary setObject:productName forKey:#"name"];
[tempArray release];
I am getting a NSException crash with this report. Please help.
The method getObjects:range: that is being sent to your NSDictionary instance is a NSArray method.
You're probably trying to to your initWithArray passing a NSDictionary instead of a NSArray.
Is the NSLog entry showing up? If not, it's because tempArray does not respond to retainCount. You don't need to worry about anything with release or retain if you're using Xcode 4.2 with ARC for iOS 5 (which you should, unless you have legacy code).
Otherwise, somewhere you're sending an object a message it doesn't respond to.

App crashes when accessing CLLocation

I have a CLLocation that I set to the current location within the following function
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation{
currentLocation = newLocation;
}
When I try to access the current position in a later method the app crashes.
NSLog(#"current latitude is %f", currentLocation.coordinate.latitude);
NSString *longitude = [NSString stringWithFormat:#"%g", currentLocation.coordinate.longitude];
NSString *latitude = [NSString stringWithFormat:#"%g", currentLocation.coordinate.latitude];
The current position is set in the first method. I can print it out, what is weird is that I have this currentLocation defined as a pointer in the header.
The app crashes and there is no stacktrace. The debugger just sends me to the retValue of my app.
I tried retaining the pointer as well as alloc before setting it to the newLocation, but nothing seems to work.
What am I missing :D
I'm not so sure about your code but I can suggest this
I tried retaining the pointer as well as alloc before setting it to the newLocation, but nothing seems to work.
You need to retain newLocation pointer not the currentLocation before setting.
currentLocation = [newLocation retain];
or
self.currentLocation = newLocation;
Because your assignment currentLocation = newLocation; does not retain the pointer and newLocation is autorelease.
The app crashes and there is no
stacktrace. The debugger just sends me
to the retValue of my app.
Are you using Xcode 4? A lot of people are having trouble with the new interface. At the bottom of the stack trace window there's a slider, if it's set to be "coarse" it'll only show the app's main() block.
As indicated in the comments above, I reckon you should be saying self.currentLocation instead of currentLocation. The difference is that when you just say currentLocation, you are only assigning a pointer. To get all the "magic" functionality that you define in the header file (retain, et al), you need to use the self.currentLocation notation, OR, [self setCurrentLocation:newLocation];. Functionally those are the same, it's a matter of style.

message sent to released object (never released manually)

Removed release statements. Some of them seemed to be okay, but that was probably just because other things were exploding first.
- (void)handleNowPlayingItemChanged:(id)notification {
MPMediaItem *item = self.musicPlayer.nowPlayingItem;
NSString *title = [item valueForProperty:MPMediaItemPropertyTitle];
NSNumber *duration = [item
valueForProperty:MPMediaItemPropertyPlaybackDuration];
float totalTime = [duration floatValue];
progressSlider.maximumValue = totalTime;
CGSize artworkImageViewSize = self.albumCover.bounds.size;
MPMediaItemArtwork *artwork = [item valueForProperty:
MPMediaItemPropertyArtwork];
if (artwork) {
self.albumCover.image = [artwork imageWithSize:artworkImageViewSize];
} else {
self.albumCover.image = nil;
}
titleLabel.text = title;
/*OpenEars stuff*/
}
In another question I mention the SQLite errors concerning artwork.
** Deleted error and details concerning NSZombieEnabled alert of call to released objects. **
Well don't I feel stupid. It was all memory management.
I put effort into not leaking anything, even in a temporary solution, and yet I did this...
In the code you provide I do not see any calls to retain, alloc/init, or some variation of copy. That means that you should not have a any calls to release in that method and that will be the cause of your crash. Make sure you are not over releasing in other methods and remember the basics of memory management.
You're releasing title and artwork, but they're not yours. This will lead, soon or later, to a tentative to release an already deallocated object (from item's dealloc or somewhere else).
// [artwork release];
//[title release];
comment those since those are autoreleased object

CLLocation memory issues

I've some memory issues with CLLocation.
CLLocation *annotation = [[CLLocation alloc] initWithLatitude:[[tempDict objectForKey:#"lat"] doubleValue] longitude:[[tempDict objectForKey:#"lon"]doubleValue]];
CLLocation *item2 = [[CLLocation alloc] initWithLatitude:[newLatString doubleValue] longitude:[newLongString doubleValue]];
cell.detailTextLabel.text = [NSString stringWithFormat:#"%.1f km",[item2 distanceFromLocation:annotation]/1000];
[annotation release];
[item2 release];
So I tried to do this, but I realised that you can't set the annotation's coordinate.
CLLocationCoordinate2D tempCoordinate = annotation.coordinate;
tempCoordinate.latitude = [[tempDict objectForKey:#"lat"] doubleValue];
tempCoordinate.longitude = [[tempDict objectForKey:#"lon"] doubleValue];
annotation.coordinate = tempCoordinate;
Is there a workaround this? I don't want to be alloc/initing a CLLocation everytime cellForRowAtIndexPath is called..
your resultant object is an NSString - just create a class which contains an NSString, as well as references/ivars of the intermediate data where necessary. then using an observer idiom, just update the cells when the string changes (design it so the string depends on the coordinates). you could probably make a class which takes a set of arguments at initialization (e.g. coordinates), creates an NSString during initialization, and then refer to the result if your data never changes. it really depends on what data you expect will mutate, and at what frequency.
I don't want to be alloc/initing a
CLLocation everytime
cellForRowAtIndexPath is called..
Why not? Do you know it's causing performance problems? You're releasing them right away, so they aren't taking up extra memory. CLLocation looks like a pretty lightweight class, and the Objective-C runtime is heavily optimized, so they probably alloc / init pretty quickly. Until you see scrolling / perf / memory issue, I would go with what works and is easy to maintain.
Premature optimization is the root of all evil - Donald Knuth

Why Instruments report a leak?

I am developing an iphone app. Instruments reported a leaked object ServiceTypes. Below is the relevant code. Does anyone have any ideas? Thanks a lot for your help.
ServiceTypes *serviceTypes = [[ServiceTypes alloc] init];
if ([userConnection getServiceTypes:serviceTypes]) {
if ([serviceTypes.types length] > 0) {
NSArray *array = [[NSArray alloc] initWithArray:[serviceTypes.types componentsSeparatedByString: SERVICE_TYPE_DELIMITOR]];
serviceRequestTypes = [[NSMutableArray alloc] initWithArray:array];
[array release];
}
}
[[self typesTableView] reloadData];
[serviceTypes release];
It doesn't look like serviceTypes is being leaked. From the code you posted, serviceTypes is always released at the end of the method, and it doesn't appear to be retained anywhere in your sample. My question is: what happens inside getServiceTypes:. Does that method retain the serviceTypes parameter?
One more thing. If serviceRequestTypes is an instance variable (and it looks like it is), then you may be leaking memory by reassigning it without releasing the existing serviceRequestTypes object first. You should either rewrite serviceRequestTypes to be a property and use a synthesized accessor or make sure to release it every time before assigning. If its current value is nil, no big deal; the release message will simply be ignored. For example:
[serviceRequestTypes release];
serviceRequestTypes = [[NSMutableArray alloc] initWithArray:[serviceTypes.types componentsSeparatedByString:SERVICE_TYPE_DELIMITER]];