【问题标题】:Adding delegate to AVAudioPlayer cause "_NSAutoreleaseNoPool(): Object 0x55e060 of class NSCFString autoreleased with no pool in place - just leaking"将委托添加到 AVAudioPlayer 导致“_NSAutoreleaseNoPool(): NSCFString 类的对象 0x55e060 自动释放,没有适当的池 - 只是泄漏”
【发布时间】:2009-06-08 21:51:10
【问题描述】:

我一直在使用一个类来使用 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 没有被调用。

有什么想法吗?我是不是突然跑到另一个线程里了?

【问题讨论】:

  • 您能否将代码缩减为触发问题的最小示例?

标签: iphone delegates avaudioplayer


【解决方案1】:

我发现了问题。当然是我的错误。

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

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

}

主要是出于好奇。

好吧,当我向我的声音添加一个委托时,这个方法会在委托之前被调用,并且它会从 AVAudioPlayer 恰好位于的任何运行循环中调用,并且很可能是一个没有自动释放池的运行循环。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多