2009-10-09 122 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設置委託以釋放委託對象?

即使我做這樣的事情:

-(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'中完成。 – 2009-10-09 17:49:21

回答

0

通常,代表不會保留,只能分配。代表應保留在其他地方。除此之外,這保持了保留週期的發生。

+0

所以這意味着我的代碼應該可以工作,但它不? – Auday 2009-10-09 17:03:03