解决潜在的内存泄漏问题

Zhe*_*hen -1 memory-leaks objective-c core-foundation abaddressbook ios

在xcode中运行分析工具后,我得到以下记忆泄漏

//Getting memeory leak warning here "Potential leak of an object allocated and stored into 'phones'
ABMultiValueRef phones = ABRecordCopyValue(ref, kABPersonPhoneProperty);

//Getting potential leak error for line below
if (ABMultiValueGetCount(ABRecordCopyValue(ref, kABPersonPhoneProperty))!=0)
{
    //Getting potential leak error for line below
    CFStringRef pNumber = ABMultiValueCopyValueAtIndex(phones,0);
    phoneNumber = [NSString stringWithFormat:@"%@", (NSString *)pNumber];
    NSString *contactFirstLast = [NSString stringWithFormat: @"%@ %@", firstName, lastName];
}
Run Code Online (Sandbox Code Playgroud)

我该如何解决这些泄漏?

X S*_*ash 5

ABMultiValueRef phones = ABRecordCopyValue(ref, kABPersonPhoneProperty);

if (ABMultiValueGetCount(phones) != 0)
{
    CFStringRef pNumber = ABMultiValueCopyValueAtIndex(phones,0);
    phoneNumber = [NSString stringWithFormat:@"%@", (NSString *)pNumber];
    NSString *contactFirstLast = [NSString stringWithFormat: @"%@ %@", firstName, lastName];
    CFRelease(pNumber);
}
CFRelease(phones);
Run Code Online (Sandbox Code Playgroud)