Memory leaks ABRECORDCYVALUE的潜在内存泄漏
我正在构建一个应用程序,要求我从iPhone地址簿加载表数据源中的所有联系人。跑步时 构建和分析 对于以下代码段Memory leaks ABRECORDCYVALUE的潜在内存泄漏,memory-leaks,abaddressbook,Memory Leaks,Abaddressbook,我正在构建一个应用程序,要求我从iPhone地址簿加载表数据源中的所有联系人。跑步时 构建和分析 对于以下代码段 ABAddressBookRef addressBook = ABAddressBookCreate(); int nPeople = ABAddressBookGetPersonCount(addressBook); CFRelease(addressBook); for(int i=0; i < nPeople; i++ ){ //ABRecordRef pe
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];
我真的厌倦了修理它,但无法修理。请帮帮我
任何形式的帮助都将受到高度赞赏
提前谢谢 您没有发布ABRecordCopyValue的结果;尝试将其分配给一个变量,然后释放它并结束循环。使用变量还可以使代码更易于阅读,并更好地突出这些问题的原因
顺便说一句,您还使用相同的参数调用了两次ABRecordCopyValue,您应该只使用上面提到的变量调用一次。您没有发布ABRecordCopyValue的结果;尝试将其分配给一个变量,然后释放它并结束循环。使用变量还可以使代码更易于阅读,并更好地突出这些问题的原因
顺便说一句,您还使用相同的参数调用了两次ABRecordCopyValue,您应该只使用上面提到的变量执行一次。我认为您可以执行以下操作:
CFTypeRef copiedValue = ABRecordCopyValue([allPeople objectAtIndex:i], kABPersonFirstNameProperty);
name = [[NSString stringWithFormat:@"%@", copiedValue] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
CFRelease(copiedValue);
我想你可以像下面这样做:
CFTypeRef copiedValue = ABRecordCopyValue([allPeople objectAtIndex:i], kABPersonFirstNameProperty);
name = [[NSString stringWithFormat:@"%@", copiedValue] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
CFRelease(copiedValue);
您可以直接桥接到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);
您可以直接桥接到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);
嘿,你能帮我解决这个问题吗?谢谢,你能帮我解决这个问题吗?因为我不确定bridge是否会复制字符串。但接下来,您将释放CF引用,然后使用NSString。这可能是内存问题吗?因为我不确定bridge是否会复制字符串。但接下来,您将发布CF引用,然后使用NSString。