views:

567

answers:

4

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 reference count should be 0. any ideas why this might be? thanks.

A: 

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.

Fletcher Moore
in the api it says for removeFromSuperview "this method releases the receiver", so i think that should take it's reference count to 0 (addSubView retains it, removeFromSuperview releases it)
jonydep
@jonydep: you're right, so the problem must be elsewhere.
Felixyz
A: 

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.

Vladimir
thanks. ok so if the code is doing what i thought, then yes logging the retain count is definitely what i need to do. i couldn't work out how to do that though, is it in the debug window somewhere?
jonydep
[anObject retainCount], but it's usually a bad idea to try to debug a program by getting the retainCount, because it's often very misleading. In this case the static analyzer might be able to help you (Build and Analyze, in XCode).
Felixyz
Yes, using NSLog(@"retainCount=%d",[myView retainCount]); is not always informative, still may help in many cases. The same with static analyser it helps when you have logic errors in memory allocation/freeing. And it can be normal behavior when view is removed from super but not dealloced. What is your problem exactly? Do you want to find what object "holds" your view or something else?
Vladimir
i wanted to find out where some kind of "retain" is happening. you were right, my subview was using a timer, where the invocation target was the view, so this did some kind of retaining i guess. if i invalidate the timer then it all deallocs ok. i previously had the invalidation of the timer IN the dealloc method, catch-22!thanks for the help.
jonydep
A: 

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

sbooth
makes sense now, cheers
jonydep
A: 

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

JeremyP
thanks for that, bookmarked..
jonydep