我是iphone应用程序的新手。现在这是我的第一个应用程序,应用已安装但未运行? 我写这段代码,它显示内存泄漏。请找出来。谢谢。
ABRecordRef ref = CFArrayGetValueAtIndex(all, i);
CFStringRef *firstName = (CFStringRef *)ABRecordCopyValue(ref, kABPersonFirstNameProperty);
NSLog(@"Name %@", firstName);
contact.strFirstName = (NSString*)firstName;
CFStringRef *lastName = (CFStringRef *)ABRecordCopyValue(ref, kABPersonLastNameProperty);
NSLog(@"Name %@", lastName);
contact.strLastName = (NSString*)lastName;
contact.contactName = [NSString stringWithFormat:@"%@ %@",(NSString *)firstName,lastName];
NSLog(@"Name %@", contact.contactName);
ABMutableMultiValueRef phoneNumbers = ABRecordCopyValue(ref, kABPersonPhoneProperty);
for(CFIndex j = 0; j < ABMultiValueGetCount(phoneNumbers); j++)
{
CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(phoneNumbers, j);
NSString *phoneNumber = (NSString *) phoneNumberRef;
contact.strMobileNo = phoneNumber;
NSLog(@"phoneNO is %@", phoneNumber);
CFRelease(phoneNumberRef);
}
ABMultiValueRef emails = ABRecordCopyValue(ref, kABPersonEmailProperty);
for(CFIndex k = 0; k < ABMultiValueGetCount(emails); k++)
{
CFStringRef emailRef = ABMultiValueCopyValueAtIndex(emails, k);
NSString *mailid = (NSString *) emailRef;
contact.strMail = mailid;
NSLog(@"Email is %@", mailid);
CFRelease(emailRef);
}
CFRelease(emails);
CFRelease(phoneNumbers);
答案 0 :(得分:2)
您在firstName和lastName上使用ABRecordCopyValue
,这意味着您还需要CFRelease
这些。
答案 1 :(得分:1)
CFRelease是一种方法(因为@Joe和@jamapag已经回答了)..我只想补充一点,XCode有一些很好的功能,比如cmd + shift + a给你一个静态内存分析器..你也可以使用run - &gt;运行w /性能工具,然后使用分配或泄漏动态分析我们的内存管理。
答案 2 :(得分:0)
您需要添加:
CFRelease(firstName);
CFRelease(lastName);