-2

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);
Deepak Danduprolu
  • 44,595
  • 12
  • 101
  • 105
Maidul
  • 1,289
  • 16
  • 30

3 Answers3

2

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

Joe
  • 56,979
  • 9
  • 128
  • 135
  • hai, i release those two firstName and lastName app was crash......so please help me....... – Maidul Jul 13 '11 at 02:55
  • You need to add `CFRelease` after the line `NSLog(@"Name %@", contact.contactName);` where were you releasing it? – Joe Jul 13 '11 at 12:44
1

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.

paxx
  • 1,079
  • 2
  • 12
  • 26
0

You need to add:

CFRelease(firstName);
CFRelease(lastName);
jamapag
  • 9,290
  • 4
  • 34
  • 28
  • hai, i release those two firstName and lastName app was crash......so please help me....... – Maidul Jul 13 '11 at 02:55