使用CFPropertyListCreateDeepCopy时为什么会出现泄漏?

时间:2010-10-12 21:32:01

标签: iphone objective-c memory-management memory-leaks nsmutabledictionary

我正在创建一个字典的深度可变副本,但由于某种原因我正在泄漏。我试过这个:

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

而且:

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

两者都被界面构建器的泄漏设备标记。

有什么想法吗?

谢谢!

4 个答案:

答案 0 :(得分:1)

我的猜测是你保留了字典中的一个对象。泄漏的字节数是多少?

答案 1 :(得分:0)

您是否真的在copyOfSectionedDictionaryByFirstLetter方法中发布了dealloc

你要么必须这样做:

self.copyOfSectionedDictionaryByFirstLetter = nil;

或者:

[copyOfSectionedDictionaryByFirstLetter release];
copyOfSectionedDictionaryByFirstLetter = nil; // Just for good style

答案 2 :(得分:0)

我怀疑NSMutableDictionary的直接案例令分析人员感到困惑。请尝试以下方法:

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

答案 3 :(得分:0)

如果您多次呼叫以下部分:

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

你应该把它改成:

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

我想这可能是你泄密的原因。