views:

156

answers:

2

The following code is creating a number of leaks which I do not understand as I release all objects created with Create or Copy. I have not worked with CF objects extensively so I may have a misunderstanding of the retention cycle. Any help would be appreciated please:

ABMutableMultiValueRef webServices = ABRecordCopyValue(aRecord, kABPersonInstantMessageProperty);

 for (CFIndex i = 0; i < ABMultiValueGetCount(webServices); i++) { 

  CFDictionaryRef webService = CFDictionaryCreateCopy(NULL, ABMultiValueCopyValueAtIndex(webServices, i));

  webServiceLabel = ABMultiValueCopyLabelAtIndex(webServices, i);

  webServiceProvider = CFDictionaryGetValue(webService, kABPersonInstantMessageServiceKey);
  webServiceUserName = CFDictionaryGetValue(webService, kABPersonInstantMessageUsernameKey);

  // Data to be saved at this point
  if (webService) CFRelease(webService);
  if (webServiceLabel) CFRelease(webServiceLabel);
 }

 if (webServices) CFRelease(webServices);
A: 

I run the simulator like this

MallocStackLogging=1 /Developer/Platforms/iPhoneSimulator.platform/Developer/Applications/iPhone\ Simulator.app/Contents/MacOS/iPhone\ Simulator

then run the app, then run leaks appname.

I put your code inside a test app and couldn't really see much going on, a few "leaks" in the form of allocations deep in foundation or cocoa code.

Could you run your app with the above and show leaks that can be traced to the method running the code above?

duncanwilcox
Hi, I ran Leaks using Instruments which indicate that the leaks are not called from the method (directly). I can only assume there must be a special way to release address book multivalue dictionaries.
Run Loop
ABMutableMultiValueRef is a CFTypeRef, so it shouldn't be necessary. I'm just thinking the leaks you're seeing aren't something you should necessarily worry about.
duncanwilcox
+3  A: 

Here's your problem:

CFDictionaryRef webService = CFDictionaryCreateCopy(NULL, ABMultiValueCopyValueAtIndex(webServices, i));

ABMultiValueCopyValueAtIndex creates an object which is never assigned to a variable. It needs to be released, but never is. That's a memory leak.

Alex
Thank you, problem solved and lesson learnt!
Run Loop