开发者

removeObjectForKey: doesn't call dealloc

I was under the impression that removing an object from an NSMutableDictionary using removeObjectForKey:@"someKey" released the object being removed. Howev开发者_开发知识库er, 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.


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];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜