Potential Memory leak abaddressbookcopyarrayofallp

2019-09-19 10:44发布

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);
    }

3条回答
小情绪 Triste *
2楼-- · 2019-09-19 11:08

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()

查看更多
够拽才男人
3楼-- · 2019-09-19 11:12

use _addressbook instead of self.addressBook.

查看更多
时光不老,我们不散
4楼-- · 2019-09-19 11:13

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(…));
查看更多
登录 后发表回答