ABRecordCopyValue 潜在的内存泄漏
我正在构建一个应用程序,要求我从 iPhone 地址簿加载表数据源中的所有联系人。跑步时
构建和分析
以下代码片段,
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];
我发现该行存在潜在的内存泄漏,
name = [[NSString stringWithFormat:@"%@", ABRecordCopyValue([allPeople objectAtIndex:i], kABPersonFirstNameProperty)] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
我真的厌倦了修复它,但无法修复。请帮助我。
任何形式的帮助将不胜感激。
提前致谢!!
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
for the following snippet
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.
Thanks in advance!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您没有发布
ABRecordCopyValue
的结果;尝试将其分配给一个变量并释放它并结束循环。使用变量还将使您的代码更易于阅读并更好地突出这些问题的原因。顺便说一句,您还使用相同的参数调用
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.BTW, you are also calling
ABRecordCopyValue
twice with the same arguments, you should only do it once (using a variable as mentioned above).我认为你可以这样做:
I think you can do like below:
您可以直接桥接到 NSString。可能更清楚一点:
You can directly bridge to an NSString. It may be a little more clear: