If you call [self dealloc] inside the -dealloc definition, then it will result in a recursion. Are you sure the code is correct? –
codelogicFeb 18 '09 at 0:36
Never call dealloc except as [super dealloc] at the end of your class's dealloc method. Therelease method relinquishes ownership of an object. When a Cocoa object no longer has any owners, it may be deallocated — in which case it will automatically be sent a dealloc message.
If you're going to program Cocoa, you need to read the Memory Management Guidelines. It's incredibly simple once you get over the initial hump, and if you don't understand what's in that document, you'll have lots of subtle bugs.
For the comment of accepted answer, it is not [self dealloc] it is [super dealloc] and he had meant to write [super dealloc] into your overrided dealloc mathod...
Welcome to stackoverflow. I have a little hint for you: If you don't like to be downvoted you should post comments as comments and not as answers. Just removed my finger from the downvote button, because I thought you shouldn't be downvoted on your first day. Have fun on SO. –
Matthias BauchDec 26 '10 at 11:11
I wonder if this is still true with ARC. If you use ARC you can never call dealloc directly, not even [super dealloc] in an overridden dealloc routine so this code is no longer correct. Actually, can you even override dealloc if you use ARC? If not, where do you clean up? –
MikeJan 10 at 19:50
@Mike Yes, you can override dealloc in ARC. You use it to clean up resources that are not managed by ARC, such as Core Foundation objects. Also - it's a useful place to remove observers etc. –
AbizernJan 17 at 11:49
You're never supposed to call dealloc explicitly (unless it's [super dealloc] within the dealloc method, but that's the only exception). Objective-C handles memory management via reference counting, so you're simply supposed to match your allocs/retains with releases/autoreleases and let the object deconstruct itself.
[self dealloc]
inside the-dealloc
definition, then it will result in a recursion. Are you sure the code is correct? – codelogic Feb 18 '09 at 0:36