Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone: Is this a leak or not

Recently someone on Stack Overflow told me that the code below does not leak, that the property handles retention itself:

self.locationManager = [[CLLocationManager alloc] init];

in dealloc :

   self.locationManager = nil;

where in the .h file:

@property (nonatomic, retain) CLLocationManager *locationManager;

I thought that was an obvious leak and believed this should fix the leak:

self.locationManager = [[[CLLocationManager alloc] init] autorelease];

However he claimed that would not work, because in his words : "you don't autorelease properties of a class. The autogenerated accessor of a property defined to retain will handle retention automatically"

And he made me wonder if he is wrong or have I not understood memory management at all?

EDIT 1: Is the code

self.myName=[NSSting stringWithFormat:@"%@ is correct.", @"TechZen"];

any different than

 self.locationManager = [[[CLLocationManager alloc] init] autorelease];

memory management-wise?

The guy says the first one is correct and refuses the second one. Why would the second one be SO WRONG? As far as I can see both assign autoreleased instances to some properties, but somehow there's still a stubborn argument that second one is wrong. I cannot see it, any help would be so welcome.

like image 717
ahmet emrah Avatar asked Feb 12 '10 15:02

ahmet emrah


People also ask

Is iPhone 15 coming out?

iPhone 15 release date Therefore, we suspect the iPhone 15 will be unveiled on September 12 or 13, 2023. At least some of the models will likely go on sale for preorder that Friday (September 15) and be available in stores the following Friday (September 22).

Who leaked the iPhone 14?

First, the leaks. The big news comes from DuanRui, a Chinese tipster with an excellent track record, who has shared real-world shots of the new iPhone 14 Pro / Pro Max display, one of which has the screen on.

Will iPhone 15 have Touch ID?

According to Bloomberg's Mark Gurman, who often reveals accurate insights into Apple's plans, Apple has tested an under-display version of ‌Touch ID‌ designed for the iPhone, but it is not expected to be present on the the ‌iPhone 15‌ or other flagship models in the foreseeable future.

How do you tell if an iPhone has been resealed?

See if your iPhone is refurbished. The first letter in the model name will tell you your iPhone's status: If the first letter is "M" or "P", your phone is an original (retail) model. If the first letter is "N", your phone was refurbished by Apple.


1 Answers

Counting retains and releases helps in this case. It is most definitely a leak. Your locationManager object will be retained 2 times: once by the alloc/init calls, and once by the property. Setting the property to nil will only release the locationManager once.

For the examples given in Edit 1, they are indeed the same. It sounds like the other developer has an aversion to immediately autoreleasing or doesn't quite understand what autorelease does.

like image 146
Colin Gislason Avatar answered Oct 17 '22 01:10

Colin Gislason