Luuk D. Jansen
Luuk D. Jansen

Reputation: 4508

Dealloc not called in superclass

I see some strange behavior which I do not understand. I have a helper class HelperClass, which is allocated and retained in a superclass SuperClass.

In the dealloc function of the superclass, I release the HelperClass. This is all fine. But when I subclass, the HelperClass is released, but on the HelperClass dealloc is not called for some reason. It does work when I release the HelperClass in the subclass.

Any ideas how this could be?

(Edit: it seems to work fine if I explicitly call [HelperClass dealloc] instead of [HelperClass release], but it this a proper way of doing this?)

@interface SuperClass : UIViewController {

    @protected
    PlayerHelper* _mediaPlayerHelper;    
}

@end

Initiated in:

- (void)viewDidLoad
{
    [super viewDidLoad];            
    // Add observer/helper for audio events
    _mediaPlayerHelper = [[[PlayerHelper alloc] init:self] retain];
}

With dealloc:

- (void) dealloc {
    if(_mediaPlayerHelper != nil) {
        [_mediaPlayerHelper release];
    }

    [super dealloc];
}

If I subclass this like:

@interface SubClass : SuperClass

And release this class, the HelperClass does not get properly dealloc-ed. It does work if I release the helper specifically in the subclass. When releasing the subclass, the dealloc of the superclass is called, but not the dealloc in the helper.

The helper is a simple NSObject class:

@interface PlayerHelper : NSObject

Upvotes: 0

Views: 178

Answers (1)

amleszk
amleszk

Reputation: 6290

you are double retaining here

[[[PlayerHelper alloc] init:self] retain];

instead use:

[[PlayerHelper alloc] init:self];

http://interfacelab.com/objective-c-memory-management-for-lazy-people/

Upvotes: 2

Related Questions