Stanley
Stanley

Reputation: 4486

NSMutableArray retain count

    target_locations[ 0] = [[CLLocation alloc] initWithLatitude :  51.50373056 
                                                      longitude :  0.129986111];
   [target_locations[ 0] release];

Consider the above code, is it the proper way to keep the assigned object to have a retain count of 1 ?

*Assuming ARC is not activated.

Upvotes: 0

Views: 150

Answers (1)

jscs
jscs

Reputation: 64022

Given that target_locations is an NSMutableArray, and that ARC is not enabled, the correct procedure here is as follows:

CLLocation * newLocation = [[CLLocation alloc] initWithLatitude :  51.50373056 
                                                      longitude :  0.129986111];
target_locations[0] = newLocation;
[newLocation release];

You shouldn't send release to the result of an array access because you don't own that object through that pointer. While it does work in this case, it's incorrect semantically and too likely to cause problems if you get into the habit.

Also, consider renaming target_locations to targetLocations, which is consistent with Cocoa style. Using the underscore makes it look like a plain-C array rather than an object.

Upvotes: 1

Related Questions