views:

82

answers:

1

I call ABAddressBookCopyPeopleWithName(book, fullname); which is supposed to return an array of ABRecords for all the names that match 'fullname' from the AddressBook. Since this has a 'Copy' in it's name, it's returning with a retain count of 1 which I should release. What if it couldn't find any matching names? Should I still release it? Is the empty object set to nil? Or is it just empty, and calling release will cause a crash?

+2  A: 

Yep, if it has "Copy" in the name, you should release it. If it returns an array of ABRecords, it's the array itself that is being retained for you. If the array is empty and no names were found, you'd still need to release it.

I haven't used this function though, and there's a chance this could be an edge case that doesn't follow the Cocoa naming conventions. To check, call the method and then say:

NSLog(@"%d", [resultArray retainCount]);

That will tell you what the retain count on the array is, so you can be 100% sure you release it as necessary.

Hope that helps!

Ben Gotow
This is not an edge case. If it were, the documentation would say so. It does not, which means you must release the returned array.
Dave DeLong
Also, checking the `retainCount` is worthless, because even if you didn't have to release the array yourself, the count would have to be greater than or equal to 1 (even if autoreleased), or else you wouldn't have an array to work with. I've yet to find a situation where checking an object's `retainCount` has ever been useful.
Dave DeLong
Makes sense. It seems to be fine when I release it in the simulator in Debug mode. Just thought I'd double-check before updating my app.
z s
Hah! You're right. I just threw together a test and I got "3" for [[NSArray alloc] init]; and "2" for [[[NSArray alloc] init] autorelease], neither of which is very helpful. Maybe the app has to go through its run loop once before the retainCounts are updated...
Ben Gotow