Find out memory leak?

自作多情 提交于 2019-12-02 23:20:46

问题


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

回答1:


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




回答2:


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.




回答3:


You need to add:

CFRelease(firstName);
CFRelease(lastName);


来源:https://stackoverflow.com/questions/6664262/find-out-memory-leak

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!