2009-09-16 5 views
0

나의 현재 테스트에서 나는 "celestialClass"로부터 상속받은 "PlanetClass"클래스를 가지고있다. 내 질문은 내 "PlanetClass"개체를 릴리스 할 때 두 dealloc 메서드를 통과하여 먼저 Planet 개체를 릴리스 한 다음 Celestial 개체를 릴리스하는 것입니다. 나는 iVar "bodyName"을 릴리스 할 수 있도록 천체에 dealloc을 추가했습니다.이 권한이 있다고 생각합니다. 확인하고 싶었습니까?상속 출시 명령?

@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 

환호 -gary-

무엇을 어떻게하는가의 dealloc을 호출 지구 클래스의 인스턴스입니다

답변

3

, 그때가 bodyName을 허용, 천상의 클래스의 dealloc을 호출

[super dealloc]; 

를 호출합니다 풀려나 라.

기본적으로 네, 맞습니다.

+0

"Celestial"[super dealloc]도 부모 NSObject도 궁극적으로 할당 해제합니까? 아니면 다른 방식으로 작동합니까? – fuzzygoat

+0

예. 모든 서브 클래스가 훌륭하게 동작하고 dealloc의 끝에서 [super dealloc]을 호출하면 NSObject는 dealloc 메서드를 호출합니다. – bobDevil

1

-dealloc과 관련된 마법은 없습니다.

[super dealloc]을 호출하면 다른 메소드의 수퍼 클래스 호출과 동일하게 작동합니다.

정확하게 [super dealloc]에 대한 호출이 항상 dealloc 메소드의 마지막 행이어야하는 이유가 무엇입니까? 그렇지 않다면 NSObject의 -dealloc을 호출하여 객체를 할당 해제하고 -dealloc에서 복귀 한 다음 자체 또는 자체 인스턴스 변수로 무언가를 시도하여 충돌을 일으킬 수 있습니다.

그래, 코드는 작성대로 정확합니다.