Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Nov 2, 2011, 08:09 AM   #1
ashokmatoria
macrumors newbie
 
Join Date: Nov 2011
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];
peopleArray doesn't release have any release/autorelease call after this statement.

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.
ashokmatoria is offline   0 Reply With Quote
Old Nov 2, 2011, 01:15 PM   #2
RonC
macrumors regular
 
Join Date: Oct 2007
Location: Chicago-area
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
RonC is offline   0 Reply With Quote
Old Nov 2, 2011, 03:00 PM   #3
jiminaus
macrumors 65816
 
Join Date: Dec 2010
Location: Sydney
Quote:
Originally Posted by RonC View Post
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.
This code snippet confirms that the returned CFArray is not (could not be?) auto-released.
jiminaus is offline   0 Reply With Quote
Old Nov 2, 2011, 06:38 PM   #4
PhoneyDeveloper
macrumors 68020
 
PhoneyDeveloper's Avatar
 
Join Date: Sep 2008
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.
PhoneyDeveloper is offline   0 Reply With Quote
Old Nov 3, 2011, 08:39 AM   #5
RonC
macrumors regular
 
Join Date: Oct 2007
Location: Chicago-area
Quote:
Originally Posted by jiminaus View Post
This code snippet confirms that the returned CFArray is not (could not be?) auto-released.
I looked at the code snippet you referenced and I don't understand what you mean here. Do you mean that instead of autorelease it should be CFRelease?

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];
it should be more like:
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
RonC is offline   0 Reply With Quote
Old Nov 3, 2011, 05:05 PM   #6
jiminaus
macrumors 65816
 
Join Date: Dec 2010
Location: Sydney
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).
jiminaus is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

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.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

Privacy / DMCA contact / Affiliate and FTC Disclosure
Copyright 2002-2013, MacRumors.com, LLC