2012-01-24 116 views
-1

我正在分析在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]; 
} 

工具,我怎样才能解决这些泄漏后得到以下memeory泄漏?

回答

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); 
3

由于pNumber已被复制,因此需要发布它:CFRelease(pNumber)

您需要重做您的if条件,以便它使用phones,然后释放phones

+0

谢谢!但是,如果(ABMultiValueGetCount(ABRecordCopyValue(ref,kABPersonPhoneProperty))!= 0)泄漏怎么办?'? – Zhen

+0

就像我说的。没有必要为了计算它们而创建新的值。使用'phones'中已有的一个 - 和该代码片段末尾的'CFRelease(phones)'。 –