tags:

views:

69

answers:

3
int someFunction (CLLocation *currentLocation) {
  double latitude = 12.3f;
  double longitude = 22.5f;
  CLLocationDistance d1 = [currentLocation distanceFromLocation:
   [[CLLocation alloc] initWithLatitude:latitude longitude:longitude]];
  return 0;
}
+3  A: 

Yes. 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.)

walkytalky
You should probably also read apple's docs on memory management - http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmRules.html#//apple_ref/doc/uid/20000994-BAJHFBGH
Whisty
+2  A: 

Yes. You alloc a CLLocation object but never release it. I'd strongly recommend reading through the the memory management guide.

Chuck
+1  A: 

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.

Kristopher Johnson
Thanks. Are @"Fsfsdfsd" constants handled differently ?
Jarsj
You don't need to worry about string constants. You don't `alloc` them -- they are always there, regardless of how many times they have been retained or released. (But it is still a good idea to do the right thing with any `NSString` variables you use, in case someday you put something there that is not a constant.)
Kristopher Johnson