-1

I am getting the following memeory leaks after running analyze tool in xcode

//Getting memeory leak warning here "Potential leak of an object allocated and stored into 'phones'
ABMultiValueRef phones = ABRecordCopyValue(ref, kABPersonPhoneProperty);

//Getting potential leak error for line below
if (ABMultiValueGetCount(ABRecordCopyValue(ref, kABPersonPhoneProperty))!=0)
{
    //Getting potential leak error for line below
    CFStringRef pNumber = ABMultiValueCopyValueAtIndex(phones,0);
    phoneNumber = [NSString stringWithFormat:@"%@", (NSString *)pNumber];
    NSString *contactFirstLast = [NSString stringWithFormat: @"%@ %@", firstName, lastName];
}

How can I resolve these leakages?

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
Zhen
  • 12,361
  • 38
  • 122
  • 199

2 Answers2

5
ABMultiValueRef phones = ABRecordCopyValue(ref, kABPersonPhoneProperty);

if (ABMultiValueGetCount(phones) != 0)
{
    CFStringRef pNumber = ABMultiValueCopyValueAtIndex(phones,0);
    phoneNumber = [NSString stringWithFormat:@"%@", (NSString *)pNumber];
    NSString *contactFirstLast = [NSString stringWithFormat: @"%@ %@", firstName, lastName];
    CFRelease(pNumber);
}
CFRelease(phones);
X Slash
  • 4,133
  • 4
  • 27
  • 35
3

Since pNumber is copied, you need to release it: CFRelease(pNumber).

You need to redo your if condition so that it uses phones, and then release phones.

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
David Dunham
  • 8,139
  • 3
  • 28
  • 41
  • thanks! But what about the leak on 'if (ABMultiValueGetCount(ABRecordCopyValue(ref, kABPersonPhoneProperty))!=0)'? – Zhen Jan 24 '12 at 23:33
  • Like I said. There is no need to make a new copy of the values just to count them. Use the one you already have in `phones` — and `CFRelease(phones)` at the end of that code snippet. – David Dunham Jan 25 '12 at 01:32