Reputation: 14113
In my appDelegate
I am using LocationManager
:
- (void)locationManager: (CLLocationManager *)manager
didUpdateToLocation: (CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
float latitude = newLocation.coordinate.latitude;
strLatitude = [NSString stringWithFormat:@"%f",latitude];
float longitude = newLocation.coordinate.longitude;
strLongitude = [NSString stringWithFormat:@"%f", longitude];
[self CheckOperation];
}
strLatitude and strLongitude are global strings. This is absolutely fine. Even on analyzing the application I don't get any memory leak. But when I profile my application I receive a memory leak at
strLatitude = [NSString stringWithFormat:@"%f",latitude];
and
strLongitude = [NSString stringWithFormat:@"%f", longitude];
of 32 bytes.
How can I fix that?
Upvotes: 0
Views: 2888
Reputation: 31642
Are you sure you're seeing a leak and not simply just an allocation?
If you do actually have a leak here then there are some potential suspects:
Are you using ARC? If not, here are some possible problems:
Are you releasing it in dealloc?
If this method gets run more than once, you aren't releasing the last value before you reassign it.
If you're not using copy semantics, and you're passing this string reference off to someone else, and they aren't properly releasing it, you'll also get a backtrack to this line.
EDIT:
(Per the comments below)
You should realize that stringWithFormat:
is allocating a string and queuing up an autorelease on it... so you need to retain it somewhere.
I assumed you were doing this somewhere because you aren't getting an "EXC_BAD_ACCESS" - but rather, supposedly, a leak.
You shouldn't be leaking an autoreleased object unless you've retained it somewhere (hence the assumption).
Given you need to be retaining it somewhere, my above suggestions are valid - each retain needs a matching release.
I do agree that you should be using properties for these strings.
It's simple to convert them - and takes care of a lot of things for you.
In your Interface:
@property (nonatomic, copy) NSString * strLatitude;
In your Implementation:
@synthesize strLatitude;
To assign:
self.strLatitude = ...
(the "self." part is important)
And make sure you set that to nil in dealloc.
Upvotes: 5