ABRecordCopyValue 潜在的内存泄漏

发布于 2024-10-17 07:23:14 字数 1049 浏览 8 评论 0原文

我正在构建一个应用程序,要求我从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

溺ぐ爱和你が 2024-10-24 07:23:14

您没有发布 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).

我纯我任性 2024-10-24 07:23:14

我认为你可以这样做:

CFTypeRef copiedValue = ABRecordCopyValue([allPeople objectAtIndex:i], kABPersonFirstNameProperty);
name = [[NSString stringWithFormat:@"%@", copiedValue] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
CFRelease(copiedValue);

I think you can do like below:

CFTypeRef copiedValue = ABRecordCopyValue([allPeople objectAtIndex:i], kABPersonFirstNameProperty);
name = [[NSString stringWithFormat:@"%@", copiedValue] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
CFRelease(copiedValue);
↘人皮目录ツ 2024-10-24 07:23:14

您可以直接桥接到 NSString。可能更清楚一点:

CFTypeRef fn_typeref = ABRecordCopyValue(person, kABPersonFirstNameProperty);
CFTypeRef ln_typeref = ABRecordCopyValue(person, kABPersonLastNameProperty);

NSString * firstName = (__bridge NSString *) fn_typeref;
NSString * lastName  = (__bridge NSString *) ln_typeref;

NSLog(@"Name:%@ %@", firstName, lastName);

CFRelease(fn_typeref);    // releasing CFTypeRef
CFRelease(ln_typeref);

// use firstName and lastName down here
NSLog(@"Name:%@ %@", firstName, lastName);

You can directly bridge to an NSString. It may be a little more clear:

CFTypeRef fn_typeref = ABRecordCopyValue(person, kABPersonFirstNameProperty);
CFTypeRef ln_typeref = ABRecordCopyValue(person, kABPersonLastNameProperty);

NSString * firstName = (__bridge NSString *) fn_typeref;
NSString * lastName  = (__bridge NSString *) ln_typeref;

NSLog(@"Name:%@ %@", firstName, lastName);

CFRelease(fn_typeref);    // releasing CFTypeRef
CFRelease(ln_typeref);

// use firstName and lastName down here
NSLog(@"Name:%@ %@", firstName, lastName);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文