最近把IOS和Android平台的cocos2d-x游戏移植到wp8平台,切几次场景之后,app就直接奔溃了,当时一直以为是angleproject导致的问题(angleproject确实有问题,使用它你纹理所占的内存量会翻倍),所以一开始就一直在找纹理的问题,经过几次检查之后,发现所有的纹理资源均已经释放,后来发现内存每次增长的量跟背景音乐文件的大小相同,所以就把目光集中到了CocosDenshion上。
出问题的代码在Audio.cpp中,出现问题的方法代码如下:
void Audio::UnloadSoundEffect(unsigned int sound)
{
if (m_engineExperiencedCriticalError) {
return;
}
if (m_soundEffects.end() == m_soundEffects.find(sound))
return;
m_soundEffects[sound].m_soundEffectSourceVoice->DestroyVoice();
m_soundEffects[sound].m_soundEffectBufferData = nullptr;
m_soundEffects[sound].m_soundEffectSourceVoice = nullptr;
m_soundEffects[sound].m_soundEffectStarted = false;
ZeroMemory(&m_soundEffects[sound].m_audioBuffer, sizeof(m_soundEffects[sound].m_audioBuffer));
m_soundEffects.erase(sound);
}
因此正确的做法应该把缓存的数据也释放掉,修改后的代码如下:
void Audio::UnloadSoundEffect(unsigned int sound)
{
if (m_engineExperiencedCriticalError) { return; }
if (m_soundEffects.end() == m_soundEffects.find(sound)) return;
m_soundEffects[sound].m_soundEffectSourceVoice->DestroyVoice();
//Added by Bloodwolf
delete []m_soundEffects[sound].soundEffectBufferData;
m_soundEffects[sound].m_soundEffectBufferData = nullptr;
m_soundEffects[sound].m_soundEffectSourceVoice = nullptr;
m_soundEffects[sound].m_soundEffectStarted = false;
ZeroMemory(&m_soundEffects[sound].m_audioBuffer, sizeof(m_soundEffects[sound].m_audioBuffer));
m_soundEffects.erase(sound);
}