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.
Call Tree Snapshot
Leak cycle Snapshot
Am I not releasing something that should be? Also I can not call CFRelease
because I have ARC enabled.
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 oftakeRetainedValue
calls that the AddressBook framework does.ABAddressBook
is deprecated in iOS 9. You'll probably benefit by moving to usingCNContactStore
.