将委托添加到AVAudioPlayer导致“_NSAutoreleaseNoPool():类NSCFString的对象0x55e060自动释放,没有池到位 - 只是泄漏”

时间:2009-06-08 21:51:10

标签: iphone delegates avaudioplayer

我一直在使用AVAudioPlayer播放声音。由于我想在播放后立即释放这些声音,我添加了一个代表。这会导致“_NSAutoreleaseNoPool():类NSCFString的对象0x55e060自动释放,没有池到位 - 只是在声音完成播放后立即泄漏”错误,但在调用-audioPlayerDidFinishPlaying之前。

以下是一些消息来源:

@interface MyAVAudioPlayer : NSObject <AVAudioPlayerDelegate> {
    AVAudioPlayer   *player;
    float           savedVolume;
    BOOL            releaseWhenDone;
}

主要类.m:

- (MyAVAudioPlayer *) initPlayerWithName: (NSString *) name;
{
    NSString *soundFilePath = [[NSBundle mainBundle] pathForResource: name ofType: @"caf"];

    NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];

    player = [[AVAudioPlayer alloc] initWithContentsOfURL: fileURL error: nil];
    [fileURL release];
    [player prepareToPlay];
    return (self);
}
- (MyAVAudioPlayer *)getAndPlayAndRelease:(NSString *)name withVolume:(float) vol;
{
    MyAVAudioPlayer *newMyAVPlayer = [self initPlayerWithName:name];
    player.volume = vol;
    [player play];
    releaseWhenDone = YES;
    [player setDelegate: self];
    return newMyAVPlayer;
}   
+ (void) getAndPlayAndReleaseAuto:(NSString *)name withVolume:(float) vol;
{
    MyAVAudioPlayer *newMyAVPlayer = [[MyAVAudioPlayer alloc] getAndPlayAndRelease:name withVolume:vol];
//  [newMyAVPlayer autorelease];
}

#pragma mark -
#pragma mark AVAudioPlayer Delegate Methods

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)playedPlayer successfully:(BOOL)flag {
    if (releaseWhenDone) {
        NSLog(@"releasing");
        [playedPlayer release];
//      [self release];
        NSLog(@"released");
    }
}

- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error {
    NSLog(@"Error while decoding: %@", [error localizedDescription] );
}

- (void)audioPlayerBeginInterruption:(AVAudioPlayer *)player {
    NSLog(@"Interrupted!");
}

- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player {
    NSLog(@"EndInterruption!");
}

- (BOOL) play;
{   
    player.currentTime = 0.0;
    return [player play];
}

评论[player setDelegate:self];使错误消失,但我的audioPlayerDidFinishPlaying不会被调用。

有什么想法?我突然在另一个线程中运行了吗?

1 个答案:

答案 0 :(得分:0)

我发现了问题。我的错,当然。

在我的很多类文件中,我添加了:

-(BOOL) respondsToSelector:(SEL) aSelector
{
    NSLog(@"Class: %@ subclass of %@, Selector: %@", [self class], [super class], NSStringFromSelector(aSelector));
    return [super respondsToSelector:aSelector];

}

主要是出于好奇。

好吧,当我在声音中添加一个委托时,会在委托之前调用此方法,并且从AVAudioPlayer碰巧进入的任何runloop调用它,并且可能是没有自动释放池的runloop。