简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM