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

发布于 2024-09-27 08:19:17 字数 606 浏览 5 评论 0原文

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

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

还有这个:

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

两者都被界面生成器的泄漏设备标记。

有什么想法吗?

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夏日落 2024-10-04 08:19:17

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

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

十二 2024-10-04 08:19:17

你真的在你的dealloc方法中释放了copyOfSectionedDictionaryByFirstLetter吗?

您必须执行以下操作:

self.copyOfSectionedDictionaryByFirstLetter = nil;

或:

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

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
赠意 2024-10-04 08:19:17

我怀疑 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);
}

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);
}
一曲琵琶半遮面シ 2024-10-04 08:19:17

如果您多次调用下面的部分:

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

我想这可能是泄漏的原因。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文