R – Inheritance release order

deallocmemoryobjective c

In my current test I have a class "PlanetClass" that inherits from "celestialClass". My question is when I release my "PlanetClass" object it goes through both dealloc methods, firstly releasing the Planet object and then the Celestial object. I added the dealloc to celestial to make sure I could release the iVar "bodyName", I think I have this right I just wanted to check?

@implementation CelestialClass
- (NSString *)bodyName {
    return [[bodyName retain] autorelease];
}
- (void)setBodyName:(NSString *)newBodyName {
    if (bodyName != newBodyName) {
        [bodyName release];
        bodyName = [newBodyName copy];
    }
}
- (void) dealloc {
    NSLog(@"_deal_CB: %@", self);
    [bodyName release];
    bodyName = nil;
    [super dealloc];
}
@end

@implementation PlanetClass
- (int *)oceanDepth {
    return oceanDepth;
}
- (void)setOceanDepth:(int *)value {
        oceanDepth = value;
}
- (void) dealloc {
    NSLog(@"_deal: %@", self);
    [super dealloc];
}
@end

cheers -gary-

Best Answer

What happens is any instance of the Planet class will call dealloc there, then it will call

[super dealloc];

Which calls the celestial class dealloc, allowing bodyName to be released.

So basically, yes, you do have it right.

Related Topic