removeObjectForKey:不调用dealloc
我的印象是,使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
removeObjectForKey
将调用release
——当然,假设您的@"someKey"
确实匹配一个对象在字典里。然而,调用release
并不能保证该对象随后会被dealloc
处理。这取决于它还有哪些其他所有权主张。由于在这种情况下,
dealloc
消息没有被发送,我们可以得出结论,其他东西有一个持续的声明。这可能是错误的结果,也可能不是错误的结果——例如,如果您还将对象传递给了某个系统组件,那么它可能非常合理地希望将您的对象保留得比您更长。如果情况并非如此,最可能的原因是按照以下方式做了一些事情:
也就是说,永远不要放弃
alloc
授予的初始所有权。相反,应该这样做:removeObjectForKey
will callrelease
-- assuming, of course, that your@"someKey"
does actually match an object in the dictionary. However, callingrelease
doesn't guarantee that the object will then getdealloc
-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:
That is, never relinquishing the initial ownership granted by
alloc
. Instead this ought to be done something like: