MASSIVE AVAudioPlayer内存泄漏 [英] MASSIVE AVAudioPlayer Memory Leaks

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

问题描述

我使用AVAudioPLayer为我的游戏做音频。我在一个独立类中的方法中有以下代码,每次我想要boing声音播放时调用。问题是,它泄漏大量的内存,到游戏变得不可玩的点。现在我不释放方法中的任何指针,因为当时我只有一个。但现在我有10个这些方法。



在tis case释放指针的最好方法是什么? (是的,我已经尝试在[boing play]之后直接释放;这解决了泄漏(显然),但是声音没有播放,所以没有意义。

   - (void)playBoing {
int x =(arc4random()%3)+1;
NSString * path = [NSString stringWithFormat:@/ boing_0%i。 aif,x];

NSString * resourcePath = [[NSBundle mainBundle] resourcePath];
resourcePath = [resourcePath stringByAppendingString:path];

AVAudioPlayer * boing = [[AVAudioPlayer alloc] initWithContentsOfURL:
[NSURL fileURLWithPath:resourcePath] error:nil];

boing.delegate = self;
boing.volume = 1;

[boing play];
}


解决方案>

有可能有一个更好的解决方案,但我有类似的问题,其中释放立即杀死我的过程。这是我采取的解决方案。再次,可能有更好的,但为了快速修复,



在您的头文件中,创建以下内容:

  AVAudioPlayer * boing; 

然后在 - (void)playBoing 像你一样,但改变

  AVAudioPlayer * boing = [[AVAudioPlayer alloc] initWithContentsOfURL:
[NSURL fileURLWithPath: resourcePath] error:nil];

  if(boing!= nil){
boing = nil;
[boing release];
}
boing = [[AVAudioPlayer alloc] initWithContentsOfURL:
[NSURL fileURLWithPath:resourcePath] error:nil];

这应该确保只有一个实例 boing 每次分配


I am using AVAudioPLayer to do the audio for my game. I have the following code in a method in a seperate class, which is called every time I want the 'boing' sound to play. Problem is, it leaks huge amounts of memory, to the point where the game becomes unplayable. Now i'm not releasing any of the pointers in the method, because at the time I only had one. But now I have 10 of these methods.

What is the best way to release the pointers in tis case? (Yes, I have tried releasing straight after [boing play];, this solves the leak (obviously) but the sound dosen't play so theres no point.

    -(void)playBoing {
        int x = (arc4random()%3)+1;
        NSString *path = [NSString stringWithFormat:@"/boing_0%i.aif", x];

        NSString* resourcePath = [[NSBundle mainBundle] resourcePath];
        resourcePath = [resourcePath stringByAppendingString:path];

        AVAudioPlayer *boing = [[AVAudioPlayer alloc] initWithContentsOfURL:
                                [NSURL fileURLWithPath:resourcePath] error:nil];

        boing.delegate = self;
        boing.volume = 1;

        [boing play];
}

解决方案

It's possible there's a better solution, but I've had similar problems where releasing right away killed my process. This is the solution I took. Again, there may be better out there, but for a quick fix, it should do the trick.

In your header file, create the following:

AVAudioPlayer *boing;

Then in -(void)playBoing, do as you did, but change

AVAudioPlayer *boing = [[AVAudioPlayer alloc] initWithContentsOfURL:
                        [NSURL fileURLWithPath:resourcePath] error:nil];

to

if (boing != nil) {
    boing = nil;
    [boing release];
}
boing = [[AVAudioPlayer alloc] initWithContentsOfURL:
         [NSURL fileURLWithPath:resourcePath] error:nil];

This should ensure that there's only one instance of boing allocated at a time

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

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