ABRecordCopyValue的潜在内存泄漏 [英] Potential Memory Leak for ABRecordCopyValue
问题描述
我正在构建一个应用程序,要求我从iPhone地址簿中加载表的数据源中的所有联系人.跑步
I am building an app that requres me to load all the contacts in the datasource of the table from the iPhone AddressBook. On running
构建和分析
Build and Analyze
以下代码段
ABAddressBookRef addressBook = ABAddressBookCreate();
int nPeople = ABAddressBookGetPersonCount(addressBook);
CFRelease(addressBook);
for(int i=0; i < nPeople; i++ ){
//ABRecordRef person = [allPeople objectAtIndex:i];
NSString *name = @"";
if(ABRecordCopyValue([allPeople objectAtIndex:i], kABPersonFirstNameProperty) != NULL)
name = [[NSString stringWithFormat:@"%@", ABRecordCopyValue([allPeople objectAtIndex:i], kABPersonFirstNameProperty)] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
[dataSource addObject: name];
}
[allPeople release];
该行可能会发生内存泄漏
I am getting a potential memory leak for the line
name = [[NSString stringWithFormat:@"%@", ABRecordCopyValue([allPeople objectAtIndex:i], kABPersonFirstNameProperty)] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
我真的很厌倦修复它,但是无法修复.请帮我.
I am really tired of fixing it but was unable to. Kindly help me out.
任何一种帮助都将得到高度重视.
Any kind of help would be highly appriciated.
提前谢谢!
推荐答案
您不会发布ABRecordCopyValue
的结果;尝试将其分配给变量,然后释放它并结束循环.使用变量还将使您的代码更易于阅读,并更好地突出显示这些问题的原因.
You aren't releasing the result of ABRecordCopyValue
; try assigning it to a variable and release it and the end of the loop. Using a variable will also make your code a lot easier to read and highlight the cause of these issues better.
顺便说一句,您也使用相同的参数调用了ABRecordCopyValue
两次,您应该只执行一次(使用如上所述的变量).
BTW, you are also calling ABRecordCopyValue
twice with the same arguments, you should only do it once (using a variable as mentioned above).
这篇关于ABRecordCopyValue的潜在内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!