SimpleAudioEngine Cocos2d 内存泄漏问题

3
我正在运行Instruments,它显示SimpleAudioEngine存在内存泄漏。附上截图。尽管截图只显示了一次,但内存泄漏发生了多次。
此外,有时它会指向以下实现(我的代码):
-(void) preloadGameSounds
{
    // pre load the background sound 

    [[SimpleAudioEngine sharedEngine] preloadEffect:@"farm_background_sound.mp3"];

    // pre load the game sounds 

    [[SimpleAudioEngine sharedEngine] preloadEffect:@"chickenlayingegg.mp3"];

    // setup ding sound 
    [[SimpleAudioEngine sharedEngine] preloadEffect:@"ding.caf"];

    // egg pop sound 
    [[SimpleAudioEngine sharedEngine] preloadEffect:@"baloonpop.wav"];

    // preload applause sound 
    [[SimpleAudioEngine sharedEngine] preloadEffect:@"applause.mp3"];

    // wrong answer sound 
    [[SimpleAudioEngine sharedEngine] preloadEffect:@"wrong_answer_sound.wav"];

}

改变场景时,我也使用以下实现来卸载声音:

-(void) unloadSoundEffects 
{

    [[SimpleAudioEngine sharedEngine] unloadEffect:@"applause.mp3"];
    //[[SimpleAudioEngine sharedEngine] unloadEffect:@"wrong_answer_sound.wav"];
    [[SimpleAudioEngine sharedEngine] unloadEffect:@"ding.caf"];

    [[SimpleAudioEngine sharedEngine] unloadEffect:@"chickenlayingegg.mp3"];
}

这个内存泄漏正在导致游戏的FPS变低,使游戏越来越慢!


嘿@azamsharp,你找到解决方法了吗?我也遇到了同样的泄漏问题。 - Kjuly
2个回答

2
来自cocosdenshion FAQ

我应该保留/释放什么?

SimpleAudioEngine、CDAudioManager和 CDSoundEngine API可以通过共享的单例实例访问。 这是Cocoa Touch和cocos2d中广泛使用的一种模式。 共享实例不应该被保留或释放。

如果您需要完全关闭 CocosDenshion并释放它使用的所有资源, 则在使用的最高级别API上调用end方法。例如,如果您正在使用 SimpleAudioEngine,则只需调用[SimpleAudioEngine end]

如果您使用CDSoundSource对象, 必须通过诸如soundSourceForFile之类的工厂方法获取它们。 返回的CDSoundSource是自动释放的,也就是说, 如果您想在当前方法的范围之外使用它,则必须保留它。 如果您保留了CDSoundSource,则在使用完毕后应释放它。


0

你是在使用模拟器运行泄漏工具吗? 我在模拟器中遇到了同样的泄漏问题,但在设备上没有。 尝试使用设备来运行泄漏工具。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接