2009-10-09 2 views
0
@implementation MyClass 

-(id) init 
{ 
    NSString *path0 = [ [NSBundle mainBundle] pathForResource:@"myfile" ofType:@"m4a" ]; 
    mSound = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:path0] error:NULL]; 
    mSound.delegate = self; 
} 

-(void) release 
{ 

    mSound.delegate = nil; //<- this line causes MyClass release function to be called recursively 
    [ mSound release ];  //<- removing the line above makes this line do the same, i.e. calls myclass release recursively 
} 

AvAudioPlayer를 해제하면 델리게이트 개체도 해제되는 것 같습니다. 델리게이트에 할당 할 때 수동으로 retain을 호출하려고했지만 도움이되지 않았습니다. 세드릭 내가 전무AvAudioPlayer에서 delegate를 nil로 설정하면 위임 객체가 해제됩니까?

어떤 생각에 대리자를 할당 할 때 바로 초기화에서

-(id) init 
{ 
    NSString *path0 = [ [NSBundle mainBundle] pathForResource:@"myfile" ofType:@"m4a" ]; 
    mSound = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:path0] error:NULL]; 
    mSound.delegate = self; 
    mSound.delegate = nil; //<- (just for test), causes MyClass release to be invoked,  
} 

내가 MyClass에의 방출을 얻을를 호출하는 : 내가 좋아하는 뭔가를 할 경우에도

?

+0

이것이 문제의 해결책은 아니지만 'release'와'dealloc'을 섞어서 사용했습니다. 인스턴스 변수를 해제하는 것은'dealloc'에서 이루어져야합니다. –

답변

0

일반적으로 대리인은 보유하지 않으며 할당 만합니다. 대의원은 다른 곳에 보관해야합니다. 무엇보다도 이것은 유지 사이클이 발생하지 않도록합니다.

+0

그래서 위의 코드가 작동해야 함을 의미하지만 실제로 사용하지는 않습니까? – Auday

관련 문제