简体   繁体   中英

Why do I get a leak when using CFPropertyListCreateDeepCopy?

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!

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

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

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);
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM