fix: memory leak

This commit is contained in:
Nomango 2023-09-25 01:24:24 +08:00
parent 48753fb39e
commit e571bf963e
2 changed files with 33 additions and 13 deletions

View File

@ -29,11 +29,31 @@ namespace audio
SoundPlayer::SoundPlayer()
: volume_(1.f)
{
class SoundCallbackFunc : public SoundCallback
{
public:
Function<void(Sound* sound)> cb_on_end;
Function<float(Sound* sound, float volume)> cb_on_volume_changed;
void OnEnd(Sound* sound) override
{
cb_on_end(sound);
}
float OnVolumeChanged(Sound* sound, float volume) override
{
return cb_on_volume_changed(sound, volume);
}
};
auto cb = MakePtr<SoundCallbackFunc>();
cb->cb_on_end = std::bind(&SoundPlayer::OnEnd, this, std::placeholders::_1);
cb->cb_on_volume_changed =
std::bind(&SoundPlayer::OnVolumeChanged, this, std::placeholders::_1, std::placeholders::_2);
callback_ = cb;
}
SoundPlayer::~SoundPlayer()
{
StopAll();
}
void SoundPlayer::Play(SoundPtr sound, int loop_count)
@ -120,10 +140,10 @@ void SoundPlayer::SetCallback(Sound* sound)
{
// add callback if not exists
auto& cbs = sound->GetCallbacks();
auto iter = std::find_if(cbs.begin(), cbs.end(), [this](const SoundCallbackPtr& ptr) { return ptr.Get() == this; });
auto iter = std::find(cbs.begin(), cbs.end(), callback_);
if (iter == cbs.end())
{
sound->AddCallback(this);
sound->AddCallback(callback_);
}
sound->ResetVolume();
}
@ -131,7 +151,7 @@ void SoundPlayer::SetCallback(Sound* sound)
void SoundPlayer::RemoveCallback(Sound* sound)
{
auto& cbs = sound->GetCallbacks();
auto iter = std::find_if(cbs.begin(), cbs.end(), [this](const SoundCallbackPtr& ptr) { return ptr.Get() == this; });
auto iter = std::find(cbs.begin(), cbs.end(), callback_);
if (iter != cbs.end())
{
*iter = nullptr; // will be removed by sound

View File

@ -36,7 +36,7 @@ KGE_DECLARE_SMART_PTR(SoundPlayer);
* \~chinese
* @brief
*/
class KGE_API SoundPlayer : public SoundCallback
class KGE_API SoundPlayer : public ObjectBase
{
public:
using SoundList = List<SoundPtr>;
@ -90,12 +90,11 @@ public:
/// @param volume 音量大小1.0 为原始音量, 大于 1 为放大音量, 0 为最小音量
void SetVolume(float volume);
public:
void OnEnd(Sound* sound) override;
float OnVolumeChanged(Sound* sound, float volume) override;
protected:
void OnEnd(Sound* sound);
float OnVolumeChanged(Sound* sound, float volume);
void SetCallback(Sound* sound);
void RemoveCallback(Sound* sound);
@ -103,9 +102,10 @@ protected:
void ClearTrash();
protected:
float volume_;
SoundList sound_list_;
SoundList trash_;
float volume_;
SoundList sound_list_;
SoundList trash_;
SoundCallbackPtr callback_;
};
/** @} */