|
|||||||
![]() |
|
|
Thread Tools | Search this Thread | Display Modes |
|
|
#1 |
|
Does ABAddressBookCopyArrayOfAllPeople returns an autorelease'd object?
I was reviewing a code piece (see below) & I want your feedback over memory leak(s) over this statement. Can you pls explain this statement? Code:
NSMutableArray *peopleArray = [[(NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook) autorelease] mutableCopy]; Thanks. I'm pretty sure that peopleArray (a mutableCopy) needs to be released explicitly before the enclosing block ends. Currently it has a memory leak due to the absence of [peopleArray release] statement. As far as inner array is concerned, I believe its right to release (or autorelease) it since that function ABAddressBookCopyArrayOfAllPeople doesn't return an autorelease'd array. Pls let me know if anyone disagree or has different view point. Last edited by robbieduncan; Nov 2, 2011 at 08:34 AM. Reason: Please use the edit post functionality instead of posting twice. |
|
|
|
0
|
|
|
#2 |
|
According to the Advanced Memory Management Programming Guide, you own any object you create, and you create an object using a method whose name begins with “alloc”, “new”, “copy”, or “mutableCopy”.
Question 1: Does peopleArray leak? Since mutableCopy begins with mutableCopy, you own it. That means you need to either pass off that ownership OR release it. I'd say your code example creates a leak if there is no subsequent release on that object AND that block exits. By a subsequent release I mean that the automatic variable peopleArray is later assigned to a property/ivar and that property/ivar is subsequently released, or similarly the containing method could return the value and presuming the containing method begins with alloc, new, copy, or mutableCopy, would imply that the returned value is owned and needs to be released. Question 2: Is autorelease on result of ABAddressBookCopyArrayOfAllPeople correct? I'd say yes, using the same argument as above - the Copy in ABAddressBookCopyArrayOfAllPeople implies that it returns a retained object, and hence releasing it is necessary. A quick look at the documentation for ABAddressBookCopyArrayOfAllPeople doesn't say one way or the other, so we are left to infer its ownership semantics from its name. Last edited by RonC; Nov 2, 2011 at 01:20 PM. Reason: minor edit |
|
|
|
0
|
|
|
#3 | |
|
Quote:
|
||
|
|
0
|
|
|
#4 |
|
The naming conventions for CF functions are analogous to those for the Objective C frameworks, but not identical. Look up Create Rule and Get Rule in the docs.
|
|
|
|
0
|
|
|
#5 | |
|
Quote:
EDIT: After looking up Get Rule and Create Rule, I think I now understand that it is exactly what you meant. Perhaps a reference should be maintained and then CFRelease'd. So, instead of the original: Code:
NSMutableArray *peopleArray = [[(NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook) autorelease] mutableCopy]; Code:
CFArrayRef *peopleRef = ABAddressBookCopyArrayOfAllPeople(addressBook) ; NSMutableArray *peopleArray = [(NSArray *)peopleRef mutableCopy]; CFRelease(peopleRef); // Do stuff with peopleArray [peopleArray release]; // could have been autorelease on mutableCopy above Last edited by RonC; Nov 3, 2011 at 08:47 AM. Reason: added clarification |
||
|
|
0
|
|
|
#6 |
|
Because, and only because, a CFArray is toll-free bridged to an NSArray, you can use autorelease or release on it. But normally, yes, for a Core Foundation type you need to call the CFRelease function (or a more more specialised release function for the type).
|
|
|
|
0
|
![]() |
|
«
Previous Thread
|
Next Thread
»
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| thread | Thread Starter | Forum | Replies | Last Post |
| Returned 13" MBA, kept 15" MBP | ckeck | MacBook Air | 16 | Aug 1, 2011 04:52 PM |
| Was going to return iPad because of factory problem but it fell and got dented | gabriellanata | iPad | 5 | Jul 17, 2011 06:27 AM |
| Returning for backlight bleeding | robbyneal | iPad | 6 | Mar 28, 2011 02:41 AM |
| Return policy amnesty on 34 day old MBP? | CAWjr | MacBook Pro | 4 | Feb 25, 2011 09:41 AM |
| Return my new Macbook Pro 15, or not? | Politis | MacBook Pro | 23 | Jan 7, 2011 03:08 PM |
All times are GMT -5. The time now is 05:47 AM.







Linear Mode

