-1

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);
    }
Krishnabhadra
  • 34,169
  • 30
  • 118
  • 167
Raon
  • 1,266
  • 3
  • 12
  • 25
  • put your relevant code here :) –  Aug 02 '13 at 06:39
  • 2
    Are you releasing this object `temp` later? – Baby Groot Aug 02 '13 at 06:40
  • Use _bridge_transfer when allocating because you are assigning to CFArray which is array of core foundation with no method to release or dealloc ther array. To land it on retain, release land of Objective C use _bridge. – iEinstein Aug 02 '13 at 06:55
  • @AshutoshMishra every CFRelease i gave shows a potentil memory leak.. – Raon Aug 02 '13 at 07:01
  • Yeah because it is core foundation framework class so there are some leaks – iEinstein Aug 02 '13 at 07:02
  • @AshutoshMishra can i solve it? – Raon Aug 02 '13 at 07:03
  • Don't worry too much it doesn't create too much problem – iEinstein Aug 02 '13 at 07:05
  • @AshutoshMishra is there any way other than changing my CFArray to NSAttay to solve this potential memory leak?? – Raon Aug 02 '13 at 07:06
  • read this http://stackoverflow.com/questions/17967515/cfstringref-to-nsstring-arc-leaking-why/17967622#17967622 – iEinstein Aug 02 '13 at 07:08
  • 1
    @AshutoshMishra: “it is core foundation framework class so there are some leaks” is a gross oversimplification. If you get an object from CF or something CF-based, you need to clean it up. CF does not necessarily mean leaks, and accepting leaks just because CF was involved is sloppy programming. – Peter Hosey Aug 03 '13 at 04:59

3 Answers3

2

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

Roozbeh Zabihollahi
  • 7,207
  • 45
  • 39
Francis F
  • 3,157
  • 3
  • 41
  • 79
0

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(…));
Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
0

use _addressbook instead of self.addressBook.

Francis F
  • 3,157
  • 3
  • 41
  • 79