开发者

not doing dealloc

i have this in the superview:

mySubView = [[MySubView alloc] init];
[self addSubview:mySubView];
[mySubView release];

then at some point later, in the sub view, this:

[self removeFromSuperview];

when i debug it, i notice that the dealloc for the subview is never called, even though i'm fairly sure the refer开发者_开发知识库ence count should be 0. any ideas why this might be? thanks.


Yes, When you send removeFromSuperview, subsequently release message sent. And each call of release decrease retain count. And in case:

MySubView * mySubView = [[MySubView alloc] init];
[window addSubview:mySubView];
[mySubView release];
[mySubView removeFromSuperview];

the result will be as you expect: after removeFromSuperview mySubView's retain count became 0, and dealloc will be called.

but in your example there is more code behind "then at some point later" and some other object (and it is not superview) retains mySubView. You can for example log retainCount value to see where your view retained/relesed.


You can't rely on retainCount to provide any useful information for debugging. In some cases it may assist you, but the Apple docs say:

Important: This method is typically of no value in debugging memory management issues. Because any number of framework objects may have retained an object in order to hold references to it, while at the same time autorelease pools may be holding any number of deferred releases on an object, it is very unlikely that you can get useful information from this method. To understand the fundamental rules of memory management that you must abide by, read “Memory Management Rules”. To diagnose memory management problems, use a suitable tool: The LLVM/Clang Static analyzer can typically find memory management problems even before you run your program. The Object Alloc instrument in the Instruments application (see Instruments User Guide) can track object allocation and destruction. Shark (see Shark User Guide) also profiles memory allocations (amongst numerous other aspects of your program).


I don't know if removeFromSuperview does this, but the pointer in the superview needs to be released. If removeFromSuperview only releases the pointer in the subview to the superview and not the pointer in the superview to the subview, that may be the source of your problem.


i'm fairly sure the reference count should be 0.

Are you? Why?

Forget retain counts. Think only in terms of object ownership because retain counts are almost completely meaningless as an aid to debugging memory management.

Apple provides lots of tools for debugging memory management issues. Use them.

This link might be helpful.

http://developer.apple.com/iphone/library/documentation/Performance/Conceptual/ManagingMemory/Articles/FindingPatterns.html

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜