Raon
Raon

Reputation: 1286

Potential Memory leak abaddressbookcopyarrayofallpeople( )

This is function to reload my address book after saving changes, the line

self.addressbook=ABAddressbookCreateWithOptions()

and

self.contactAdd=ABAddressBookCopyArrayOfAllPeople(self.addressBook)

are showing as the potential memory leak points.

contactAdd is of type CFArrayRef and address book is ABAddressBookRef

  -(void)reloadAddressBook
    {
    //   if(self.addressBook)
    //       CFRelease(self.addressBook);
       self.addressBook = ABAddressBookCreateWithOptions(NULL,NULL);
        if(ABAddressBookHasUnsavedChanges(self.addressBook))
        {

            ABAddressBookSave(self.addressBook,NULL);
        }
    //    if(self.contactAdd)
    //        CFRelease(self.contactAdd);

        self.contactAdd=ABAddressBookCopyArrayOfAllPeople(self.addressBook);
    }

Upvotes: -1

Views: 891

Answers (3)

Francis F
Francis F

Reputation: 3285

use another variable to assign like this

contactAddtemp=ABAddressBookCopyArrayOfAllPeople(self.addressBook); 
self.contactAdd=(__bridge_retained CFArrayRef) CFBridgingRelease(contactAddtemp); 

It worked for me in xcode 4.2 but when I checked it doesnt work in 4.6 may be cause it uses ABAddressBookCreateWithOptions(NULL,NULL) instead of ABAddressBookCreate()

Upvotes: 2

Francis F
Francis F

Reputation: 3285

use _addressbook instead of self.addressBook.

Upvotes: 0

Peter Hosey
Peter Hosey

Reputation: 96323

In Core Foundation, Create and Copy functions return an ownership reference.

You need to balance that out by calling CFRelease on the returned object (the Address Book, the array of people, and anything else you're getting from such functions), or by casting it with __bridge_transfer (or by calling CFBridgingRelease):

self.addressBook = CFBridgingRelease(ABAddressBookCreateWithOptions(…));

Upvotes: 0

Related Questions