2013-01-02 86 views
5

我工作圍繞通訊錄框架時,我發現了不同類型的一些內存泄漏在我的應用程序:的iOS - 內存泄露通訊錄

Leaked Object # Address Size Responsible Library Responsible Frame 
__NSCFArray 8 <multiple> 256 Bytes AddressBook ABCMultiValueInsertAndCreateIdentifier 
__NSCFString 7 <multiple> 224 Bytes AppSupport _sqliteStatementApplyValuesFromRecordWithNullValue 
Malloc 32 Bytes 8 <multiple> 256 Bytes AddressBook ABCMultiValueInsertAndCreateIdentifier 
__NSCFArray 8 <multiple> 256 Bytes AddressBook ABCMultiValueInsertAndCreateIdentifier 
ABCMultiValue 8 <multiple> 256 Bytes AddressBook ABCMultiValueCreate 
Malloc 32 Bytes 7 <multiple> 224 Bytes AddressBook ABCMultiValueInsertAndCreateIdentifier 
__NSCFArray 7 <multiple> 224 Bytes AddressBook ABCMultiValueInsertAndCreateIdentifier 
Malloc 32 Bytes 5 <multiple> 160 Bytes AddressBook ABCMultiValueInsertAndCreateIdentifier 

這裏是我的代碼:

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person { 

    SDAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; 

    ABMultiValueRef multiRef = ABRecordCopyValue(person, kABPersonPhoneProperty); 
    NSString *number = (__bridge NSString *) ABMultiValueCopyValueAtIndex(multiRef, 0); 
    NSString *firstname = (__bridge NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty); 
    NSString *lastname = (__bridge NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty); 

    number = (number ? number : @""); 
    firstname = (firstname ? firstname : @""); 
    lastname = (lastname ? lastname : @""); 

    NSDictionary *dic = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:number, firstname, lastname, nil] forKeys:[NSArray arrayWithObjects:kSDPhoneNumberKey, kSDFirstnameKey, kSDLastnameKey, nil]]; 

    NSMutableArray *numberArray = [NSMutableArray arrayWithArray:appDelegate.contactArray]; 
    [numberArray addObject:dic]; 

    [appDelegate setContactArray:numberArray]; 

    [self.tableView reloadData]; 

    [self dismissModalViewControllerAnimated:YES]; 

    return NO; 
} 

做別人知道哪些線路是造成這些泄漏的原因?

回答

5

一般而言,名稱中含有CopyCreate的任何Core Foundation方法應將所有權轉讓給您的ARC對象,或者您必須自行撥打CFRelease。因此,您應該爲您的三個NSString對象轉讓所有權,並手動發佈multiRef

爲了將所有權轉讓給ARC你應該使用CFBridgingRelease,它根據WWDC 2012 - Modern Objective-C(約37:35到視頻),現優於以前的技術,__bridging_transfer(儘管,在幕後,它們是相同的事情)。

無論如何,你的三個NSString對象的聲明應該是:

NSString *number = CFBridgingRelease(ABMultiValueCopyValueAtIndex(multiRef, 0)); 
NSString *firstname = CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNameProperty)); 
NSString *lastname = CFBridgingRelease(ABRecordCopyValue(person, kABPersonLastNameProperty)); 

這相當於:

NSString *number = (__bridge_transfer NSString *) ABMultiValueCopyValueAtIndex(multiRef, 0); 
NSString *firstname = (__bridge_transfer NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty); 
NSString *lastname = (__bridge_transfer NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty); 

另外,不要忘記釋放multiRef,太:

CFRelease(multiRef); 

順便說一下,如果你運行了靜態分析器,我相信它會爲你指出這些。從「產品」菜單中選擇「分析」,或按移位 + 命令 + B

+0

哇,非常感謝!沒有更多的內存泄漏和分析工具是驚人的,我不知道它在那裏! – Yaman