Search code examples
iphoneobjective-ciosmemory-leaksabaddressbook

Memory leak when declaring NSString from ABRecordCopyValue


I am using the following line of code...

NSString *clientFirstName = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);

The 'analyse' feature on Xcode is saying that this giving rise to a potential memory leak. I am not releasing clientFirstName at all as I have neither alloc or retain'd it.

However, I am conscious that ABRecordCopyValue may not be returning an object as say a command like [NSMutableArray arrayWithArray:someArray] would which might mean I am indeed creating a new object that I control and must release.

Keen to hear thoughts...


Solution

  • Any sort of copy returns an object with a retainCount of 1, so you need to release it via CFRelease().

    See the doc:

    You are responsible for releasing this object.