removeObjectForKey:不调用dealloc

发布于 2024-11-01 00:44:02 字数 132 浏览 1 评论 0原文

我的印象是,使用removeObjectForKey:@“someKey”从 NSMutableDictionary 中删除对象会释放被删除的对象。但是,当我执行此操作时,不会调用对象的 dealloc 方法。我必须明确释放它们吗?感谢您的任何帮助。

I was under the impression that removing an object from an NSMutableDictionary using removeObjectForKey:@"someKey" released the object being removed. However, the dealloc method of my objects is not being called when I do this. Do I have to explicitly release them? Thanks for any help.

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

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

发布评论

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

评论(1

衣神在巴黎 2024-11-08 00:44:02

removeObjectForKey 调用release——当然,假设您的@"someKey"确实匹配一个对象在字典里。然而,调用release并不能保证该对象随后会被dealloc处理。这取决于它还有哪些其他所有权主张。

由于在这种情况下,dealloc 消息没有被发送,我们可以得出结论,其他东西有一个持续的声明。这可能是错误的结果,也可能不是错误的结果——例如,如果您还将对象传递给了某个系统组件,那么它可能非常合理地希望将您的对象保留得比您更长。

如果情况并非如此,最可能的原因是按照以下方式做了一些事情:

[dictionary setObject:[[SomeClass alloc] init] forKey:@"someKey"];

也就是说,永远不要放弃 alloc 授予的初始所有权。相反,应该这样做:

[dictionary setObject:[[[SomeClass alloc] init] autorelease] forKey:@"someKey];

removeObjectForKey will call release -- assuming, of course, that your @"someKey" does actually match an object in the dictionary. However, calling release doesn't guarantee that the object will then get dealloc-ed. It depends what other ownership claims there are on it.

Since in this case the dealloc message isn't getting sent, we can conclude that something else has a continuing claim. This may or may not be the result of an error -- for example, if you have also passed the object to some system component, it might quite legitimately want to keep your object around longer than you do.

If that isn't the case, the most likely cause would be having done something along these lines:

[dictionary setObject:[[SomeClass alloc] init] forKey:@"someKey"];

That is, never relinquishing the initial ownership granted by alloc. Instead this ought to be done something like:

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