int someFunction (CLLocation *currentLocation) {
double latitude = 12.3f;
double longitude = 22.5f;
CLLocationDistance d1 = [currentLocation distanceFromLocation:
[[CLLocation alloc] initWithLatitude:latitude longitude:longitude]];
return 0;
}
views:
69answers:
3Yes. You are allocating a second CLLocation
to pass to distanceFromLocation
and not releasing it.
You could instead do:
CLLocationDistance d1 = [currentLocation distanceFromLocation:
[[[CLLocation alloc] initWithLatitude:latitude longitude:longitude] autorelease]];
(Or various other things. See the docs, as @whisty suggests in the comments.)
(Also, it seems a bit pointless to make the call and assign its value to d1
and then just throw that away.)
Yes. You alloc a CLLocation object but never release it. I'd strongly recommend reading through the the memory management guide.
Yes. Objective-C's memory management can seem complicated, but remembering one fundamental rule will take you far:
You take ownership of an object if you create it using a method whose name begins with “alloc” or “new” or contains “copy” (for example, alloc, newObject, or mutableCopy), or if you send it a retain message. You are responsible for relinquishing ownership of objects you own using release or autorelease. Any other time you receive an object, you must not release it.
You call alloc
without a release
or autorelease
, so you are breaking the rule.