找出内存泄漏吗?

发布于 2024-11-19 18:19:23 字数 1421 浏览 5 评论 0原文

我是 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);

i am new on iphone apps.Now this is my first app,app is installed but not run?
I write this code it shows memory leak.please find out.Thanks in advance.

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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

傾城如夢未必闌珊 2024-11-26 18:19:23

您在firstName 和lastName 上使用ABRecordCopyValue,这意味着您还需要CFRelease 这些。

You are using ABRecordCopyValue on firstName and lastName which means you need to CFRelease those as well.

情绪操控生活 2024-11-26 18:19:23

CFRelease 是一种可行的方法(@Joe 和 @jamapag 已经回答了).. 我想补充一点,XCode 有一些不错的功能,例如 cmd + shift + a 提供 ua 静态内存分析器.. 你也可以使用 run - >运行性能工具,然后使用分配或泄漏来动态分析我们的内存管理。

CFRelease is a way to go (as @Joe and @jamapag already answered).. I would just like to add that XCode has few nice features like cmd + shift + a gives u a static memory analyser.. And you can also use run -> run w/ performance tools and then use allocations or leaks to analyse our memory management dynamically.

北座城市 2024-11-26 18:19:23

您需要添加:

CFRelease(firstName);
CFRelease(lastName);

You need to add:

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