From e571bf963ea71ca3e6edf2dbb1f4c53f9595d855 Mon Sep 17 00:00:00 2001 From: Nomango Date: Mon, 25 Sep 2023 01:24:24 +0800 Subject: [PATCH] fix: memory leak --- src/kiwano-audio/SoundPlayer.cpp | 28 ++++++++++++++++++++++++---- src/kiwano-audio/SoundPlayer.h | 18 +++++++++--------- 2 files changed, 33 insertions(+), 13 deletions(-) diff --git a/src/kiwano-audio/SoundPlayer.cpp b/src/kiwano-audio/SoundPlayer.cpp index b223744f..737dbaeb 100644 --- a/src/kiwano-audio/SoundPlayer.cpp +++ b/src/kiwano-audio/SoundPlayer.cpp @@ -29,11 +29,31 @@ namespace audio SoundPlayer::SoundPlayer() : volume_(1.f) { + class SoundCallbackFunc : public SoundCallback + { + public: + Function cb_on_end; + Function 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(); + 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 diff --git a/src/kiwano-audio/SoundPlayer.h b/src/kiwano-audio/SoundPlayer.h index 66e53aa7..baca0034 100644 --- a/src/kiwano-audio/SoundPlayer.h +++ b/src/kiwano-audio/SoundPlayer.h @@ -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; @@ -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_; }; /** @} */