XCode Memory Leak Issues

234 Views Asked by At

I'm nearing the end of my project, though after Analyzing my project in XCode it is indicating to me that there is a memory leak at this line:

http://i.imgur.com/uTkbA.png

Here is the text version of the relevant code:

- (void)displayPerson:(ABRecordRef)person
{
    NSString* firstName = (__bridge_transfer NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);

    NSString *lastName = (__bridge_transfer NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty);


    NSMutableString *fullName = [NSString stringWithFormat:@"%@ %@", firstName, lastName];

    //NSLog(@"%@", fullName);

    NSString* phoneNum = nil;
    ABMultiValueRef phoneNumbers;
    phoneNumbers = ABRecordCopyValue(person,
                                                     kABPersonPhoneProperty);
    if (ABMultiValueGetCount(phoneNumbers) > 0) {
        phoneNum = (__bridge_transfer NSString*) ABMultiValueCopyValueAtIndex(phoneNumbers, 0);
    } else {
        phoneNum = @"Unknown";
    }

    NSLog(@"First name is %@ and last name is %@", firstName, lastName);
    NSLog(@"Phone is %@", phoneNum);

    phoneNum = [phoneNum stringByReplacingOccurrencesOfString:@"(" withString:@""];
    phoneNum = [phoneNum stringByReplacingOccurrencesOfString:@")" withString:@""];

Can anyone help me out with this? I don't believe it's crippling, but I don't want to give Apple a reason to reject my app from the store. Thank you.

Best...SL

1

There are 1 best solutions below

1
On BEST ANSWER

You are using __bridge_transfer everywhere except for the phoneNumbers return value from ABRecordCopyValue.

You need to transfer ownership of phoneNumbers to ARC or manually release the memory.

UPDATE: Having looked at this issue a bit closer I'm not sure you can transfer ownership to ARC, see __bridge_transfer and ABRecordCopyValue: and ARC for more details.

Adding CFRelease(phoneNumbers) will manually release the memory.

For example:

NSString* phoneNum = nil;
ABMultiValueRef phoneNumbers;
phoneNumbers = ABRecordCopyValue(person,
                                                 kABPersonPhoneProperty);
if (ABMultiValueGetCount(phoneNumbers) > 0) {
    phoneNum = (__bridge_transfer NSString*) ABMultiValueCopyValueAtIndex(phoneNumbers, 0);
} else {
    phoneNum = @"Unknown";
}

CFRelease(phoneNumbers);