AVAudioPlayer 内存泄漏 [英] AVAudioPlayer memory leak

查看:27
本文介绍了AVAudioPlayer 内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一些与 AVAudioPlayer 相关的奇怪内存泄漏问题,在尝试了所有想到的方法后我需要帮助.

I'm stuck on some weird memory leak problem related to the AVAudioPlayer and I need help after trying everything that came to mind.

这是问题的简短描述 - 代码紧随其后.我初始化我的播放器并开始无限循环播放音轨(无限循环或一次播放并没有改变问题).音乐开始几秒钟后,我切换到另一个音轨,因此我创建了一个新播放器,初始化它,释放旧播放器(正在播放),然后将新播放器放置到位并播放.

Here is the short description of the problem - code appears right after. I initialize my player and start to play the sound track in an endless loop (and endless loop or one time play did not change the problem). Several seconds after the music started, I switch to another sound track, hence I create a new player, initialize it, release the old one (which is playing) and then set the new one in place and play it.

在那个时间点(就在我调用新播放器之后 - [播放器播放])我得到了内存泄漏(3.5Kb).

At that point in time (right after I call the new Player - [Player play]) I get a memory leak (of 3.5Kb).

我尝试了以下方法:

  • 停止旧播放器然后释放它 - 没有效果

  • Stop the old player and then release it - no effect

在播放指令后立即释放播放器 - 未开始播放

Release the Player right after the play instruction - did not start playing

释放两次老玩家 - 崩溃

Release twice the old player - crash

当我创建和播放第一个 Player 时不会发生内存泄漏!

Memory leak DOES NOT happen when I create and play the first Player!

此外,在参考文献中确实说播放"是异步的,因此可能会将引用计数增加 1,但在这种情况下,为什么 [Player stop] 没有帮助?

Also, in the reference it does say that the 'play' is async and so probably it increases the ref count by 1, but in this case, why didn't [Player stop] help?

谢谢,

以下是关于我如何使用它的代码的一些部分:

Here are some parts of the code about how I use it:

- (void) loadAndActivateAudioFunction {
NSBundle        *mainBundle = [NSBundle mainBundle];
NSError         *error;
NSURL           *audioURL = [NSURL fileURLWithPath:[mainBundle pathForResource: Name ofType: Type]];
AVAudioPlayer   *player = [(AVAudioPlayer*) [AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:&error];

if (!player) {
    DebugLog(@"Audio Load Error: no Player: %@", [error localizedDescription]);
    DuringAudioPrep = false;
    return;
}
[self lock];
[self setAudioPlayer: player];
[self ActivateAudioFunction];
[self unlock];

}

- (void) setAudioPlayer : (AVAudioPlayer *) player {
if (Player)
{
    if ([Player isPlaying] || Repeat)  // The indication was off???
        [Player stop];
    [Player release];
}
Player = player;

}

- (void) ActivateAudioFunction {
[Player setVolume: Volume];
[Player setNumberOfLoops: Repeat];    
[Player play];

DuringAudioPrep = false;

}

推荐答案

这里是创建 AVAudioPlayer 而不会导致内存泄漏的方法.请参阅此页面的说明.

Here is method to create AVAudioPlayer without causing memory leaks. See this page for explaination.

我已在我的应用中确认这消除了我的 AVAudioPlayer 泄漏 100%.

I have confirmed in my app that this removed my AVAudioPlayer leaks 100%.

- (AVAudioPlayer *)audioPlayerWithContentsOfFile:(NSString *)path {
    NSData *audioData = [NSData dataWithContentsOfFile:path];
    AVAudioPlayer *player = [AVAudioPlayer alloc];
    if([player initWithData:audioData error:NULL]) {
        [player autorelease];
    } else {
        [player release];
        player = nil;
    }
    return player;
}

这篇关于AVAudioPlayer 内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆