views:

83

answers:

4

I am creating a deep mutable copy of a dictionary but for some reason am getting a leak. I've tried this:

NSMutableDictionary *mutableCopy = (NSMutableDictionary *)CFPropertyListCreateDeepCopy(kCFAllocatorDefault, sectionedDictionaryByFirstLetter, kCFPropertyListMutableContainers);
self.copyOfSectionedDictionaryByFirstLetter = mutableCopy;
CFRelease(mutableCopy);

And this:

copyOfSectionedDictionaryByFirstLetter = (NSMutableDictionary *)CFPropertyListCreateDeepCopy(kCFAllocatorDefault, sectionedDictionaryByFirstLetter, kCFPropertyListMutableContainers);

Both are being flagged by interface builder's leaks device.

Any ideas?

Thanks!

A: 

Do you actually release copyOfSectionedDictionaryByFirstLetter in your dealloc method?

You will either have to do:

self.copyOfSectionedDictionaryByFirstLetter = nil;

Or:

[copyOfSectionedDictionaryByFirstLetter release];
copyOfSectionedDictionaryByFirstLetter = nil; // Just for good style
St3fan
Yes, I do release it. The leak shows up whenever a method is called in my appDelegate that runs the code in my question above. I'm really scratching my head over this one.
Jonah
+1  A: 

My guess is you are retaining one of the objects in the dictionary. What is the number of leaked bytes?

Nick H247
That seems to have been it. I'm going to continue debugging for a couple of days to make sure. Thanks!
Jonah
A: 

I suspect the immediate case to NSMutableDictionary is confusing the profiler. Try the following:

CFMutableDictionaryRef mutableCopy = CFPropertyListCreateDeepCopy(kCFAllocatorDefault, sectionedDictionaryByFirstLetter, kCFPropertyListMutableContainers);
if (mutableCopy) {
    // NOTE: you MUST check that CFPropertyListCreateDeepCopy() did not return NULL.
    // It can return NULL at any time, and then passing that NULL to CFRelease() will crash your app.
    self.copyOfSectionedDictionaryByFirstLetter = (NSMutableDictionary *)mutableCopy;
    CFRelease(mutableCopy);
}
Jonathan Grynspan
A: 

If you are calling part below multiple times:

NSMutableDictionary *mutableCopy = (NSMutableDictionary *)CFPropertyListCreateDeepCopy(kCFAllocatorDefault, sectionedDictionaryByFirstLetter, kCFPropertyListMutableContainers);
self.copyOfSectionedDictionaryByFirstLetter = mutableCopy;
CFRelease(mutableCopy);

you should change it to:

NSMutableDictionary *mutableCopy = (NSMutableDictionary *)CFPropertyListCreateDeepCopy(kCFAllocatorDefault, sectionedDictionaryByFirstLetter, kCFPropertyListMutableContainers);
[self.copyOfSectionedDictionaryByFirstLetter release];
self.copyOfSectionedDictionaryByFirstLetter = mutableCopy;
CFRelease(mutableCopy);

I guess that can be your reason for the leak.

Deniz Mert Edincik