SimpleAudioEngine Cocos2d 内存泄漏

发布于 2024-10-21 00:50:24 字数 1283 浏览 1 评论 0原文

我正在运行 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 变低,并使游戏速度越来越慢!

I am running Instruments and it indicates that the SimpleAudioEngine is leaking memory. The screenshot is attached. The memory leak is multiple times although the screenshot only shows one instance.
enter image description here

Also, sometimes it points to the following implementation (my code):

-(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"];

}

When changing the scenes I also unload the sound using the following implementation:

-(void) unloadSoundEffects 
{

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

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

This memory leak is making the FPS of the game to go low and making game slower and slower!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

時窥 2024-10-28 00:50:24

来自 cocosdenshion 常见问题解答

我应该保留/释放什么?

SimpleAudioEngine、CDAudioManager 和
CDSoundEngine API 均已访问
通过共享单例实例。
这是常用的模式
贯穿 Cocoa Touch 和 cocos2d。
共享实例不应该是
保留或释放。

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

如果您使用 CDSoundSource 对象,您
必须通过其中之一获得它们
工厂方法,例如
soundSourceForFile。 CD音源
返回的是自动释放的,即
意思是如果你想在外面使用它
您当前方法的范围
必须保留它。如果您保留
CDSoundSource 你应该释放它
当您使用完它时。

From the cocosdenshion FAQ:

What should I retain/release?

SimpleAudioEngine, CDAudioManager and
CDSoundEngine APIs are all accessed
through a shared singleton instance.
This is a common pattern that is used
throughout Cocoa Touch and cocos2d.
The shared instance should not be
retained or released.

If you need to completely shut down
CocosDenshion and free all resources
it was using then call the end method
on the highest level API you are
using. For example if you are using
SimpleAudioEngine then just call
[SimpleAudioEngine end]
.

If you use CDSoundSource objects you
must obtain them through one of the
factory methods such as
soundSourceForFile. The CDSoundSource
that is returned is autoreleased, that
means if you want to use it outside
the scope of the current method you
must retain it. If you retain a
CDSoundSource you should release it
when you are finished using it.

找个人就嫁了吧 2024-10-28 00:50:24

您是否使用模拟器来运行泄漏工具?
我在模拟器中遇到相同的泄漏,但在设备上却没有。
尝试使用设备运行泄漏工具

are you using simulator to run the leak tools ?
i encounter the same leak in simulator but not on device.
try using device to run the leak tools

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文