2012-01-24 171 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)'。 –