델리게이트를 저장하는 프로퍼티의 설정은 포인터이기 때문에 retain으로 설정하기 쉬운때 이럴경우 호출한 객체가 제대로 해제가 안되는 상황이 되어 메모리 릭 상황이 발생하게 된다.

이유는 호출한 객체를 델리게이트에 저장하면서 retain으로 메모리를 붙잡게되어

호출한 객체가 해제될 때 retain으로 붙잡혀 있어 제대로 해제가 안되는 경우가 생긴다

예를 들어

NCAppImageEffectManager * imageManager = [[NCAppImageEffectManager alloc]initWithDelegate:self];
       
self.effectManager = imageManager;
       
[imageManager release];

위와 같은 소스에서 NCAppImageEffectManager 에 델리게이트 함수로 자신을 보내게 되는데 NCAppImageEffectManager 내에서 델리게이트를 저장할 때 retain을 추가해버리면 제대로 해제가 안된다.

@property (nonatomic, retain) id<NCAppImageEffectDelegate>  controllerDelegate;

-(id) initWithDelegate:(id<NCAppImageEffectDelegate>) delegate{
   
    self=[super init];
   
    if(self != nil) {
        self.controllerDelegate = delegate;
    }
   
    return self;
}

즉 델리게이트 성격의 프로퍼티를 지정할 때는 호출한 상위객체가 해제 안된다는 가정하에 실행이 되어야 하며 assign 속성으로 지정하여 따로 retainCount를 늘리지 말아야한다.

'iOS' 카테고리의 다른 글

싱글 탭 / 더블 탭 / 핀치 GestureRecognizer 추가  (0) 2011.07.08
UIView 변형 방법  (0) 2011.06.24
UIImage 카메라 롤에 추가  (0) 2011.06.15
NSFileManager 디렉토리 위치 정의  (0) 2011.06.11
objective C (xcode4) gcov 설정방법  (0) 2011.06.07

+ Recent posts