0

I am implementing ABAddressBook in iOS 8 and I started by creating a sample project to get going. I created the project using the Single View Application template. In the viewDidAppear of the ViewController.swift ViewController I added the code below:

if ABAddressBookGetAuthorizationStatus() != .Authorized {
    return
}
var error: Unmanaged<CFError>?
if let addressBook: ABAddressBook? = ABAddressBookCreateWithOptions(nil, &error)?.takeRetainedValue() {
    ABAddressBookRequestAccessWithCompletion(addressBook) { granted, error in
        if error == nil && granted {
            if let contacts = ABAddressBookCopyArrayOfAllPeople(addressBook)?.takeRetainedValue() as? NSArray {
                for contact in contacts {
                    print("new contact")
                }
            }
        }
    }
} else {
    print(error?.takeRetainedValue())
}

This is the only code I have and the app is leaking! I am running the Leaks tool and it does not give good enough context on why the app is leaking.

Leak Tool Snapshot Memory leaks tool snapshot

Call Tree Snapshot

Memory leak call tree snapshot

Leak cycle Snapshot

Memory leak cycle snapshot

Am I not releasing something that should be? Also I can not call CFRelease because I have ARC enabled.

3
  • This code looks good to me. If you accidentally used takeUnretainedValue somewhere, that could leak, but I don't see that anywhere in your code snippet. iOS does have a few of these tiny leaks scattered about, so I wouldn't worry about them unless it's large or you see any of your classes not get deallocated. By the way, if possible, consider using the Contacts framework, which is easier to use and doesn't require the fragile process of takeRetainedValue calls that the AddressBook framework does.
    – Rob
    Jun 9, 2016 at 20:54
  • 1
    ABAddressBook is deprecated in iOS 9. You'll probably benefit by moving to using CNContactStore.
    – gjeck
    Jun 9, 2016 at 22:16
  • @gjeck I am supporting iOS 8 devices so I can not use CNContactStore on iOS 8 devices. Jun 9, 2016 at 22:24

1 Answer 1

0

I was unable to reproduce but for what it's worth the following produced no leaks either:

var addressBook: ABAddressBookRef?

func extractABAddressBookRef(abRef: Unmanaged<ABAddressBookRef>!) -> ABAddressBookRef? {
    if let ab = abRef {
        return Unmanaged<NSObject>.fromOpaque(ab.toOpaque()).takeUnretainedValue()
    }
    return nil
}

func fetchAllContacts(completion: ((contacts: [String]?) -> Void)?) {
    let authorizationStatus = ABAddressBookGetAuthorizationStatus()
    switch authorizationStatus {
    case .NotDetermined:
        print("requesting access...")
        var errorRef: Unmanaged<CFError>? = nil
        addressBook = extractABAddressBookRef(ABAddressBookCreateWithOptions(nil, &errorRef))
        ABAddressBookRequestAccessWithCompletion(addressBook, { [weak self] success, error in
            if success {
                completion?(contacts: self?.getContactNames())
            }
            else {
                print("error")
                completion?(contacts: nil)
            }
        })
    case .Denied, .Restricted:
        print("access denied")
        completion?(contacts: nil)
    case .Authorized:
        print("access granted")
        completion?(contacts: self.getContactNames())
    }
}

func getContactNames() -> [String]? {
    var errorRef: Unmanaged<CFError>?
    addressBook = extractABAddressBookRef(ABAddressBookCreateWithOptions(nil, &errorRef))
    let contactList: NSArray = ABAddressBookCopyArrayOfAllPeople(addressBook).takeRetainedValue()
    print("records in the array \(contactList.count)")

    return contactList.map({ (record: AnyObject) -> String in
        let contactRef: ABRecordRef = record
        let contactName: String = ABRecordCopyCompositeName(contactRef).takeRetainedValue() as String
        return contactName
    })
}

override func viewDidLoad() {
    super.viewDidLoad()

    fetchAllContacts { (contacts) in
        print("fetched contacts \(contacts)")
    }
}
1
  • 1
    You should use takeRetainedValue, not takeUnretainedValue, when getting the AddressBookRef. You also don't need to do the fromOpaque/toOpaque dance. (We had to do that in early Swift betas, but not anymore.)
    – Rob
    Jun 11, 2016 at 21:56

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.