From 3c1e36c2c39d5db737c4869288af7321eedef80a Mon Sep 17 00:00:00 2001 From: James Chen Date: Thu, 13 Apr 2017 17:58:49 +0800 Subject: [PATCH] Cleanup AudioEngine resource after exiting NewAudioEngineTest. (#17675) * Unschedules update in AudioEngine implementation while AudioEngine::end is called. * Cleanup AudioEngine resource after exiting NewAudioEngineTest. --- cocos/audio/apple/AudioEngine-inl.mm | 6 ++++++ cocos/audio/win32/AudioEngine-win32.cpp | 6 ++++++ cocos/audio/winrt/AudioEngine-winrt.cpp | 2 ++ .../Classes/NewAudioEngineTest/NewAudioEngineTest.cpp | 6 ++++++ .../Classes/NewAudioEngineTest/NewAudioEngineTest.h | 3 +++ 5 files changed, 23 insertions(+) diff --git a/cocos/audio/apple/AudioEngine-inl.mm b/cocos/audio/apple/AudioEngine-inl.mm index 0da35dda6a..72936d1684 100644 --- a/cocos/audio/apple/AudioEngine-inl.mm +++ b/cocos/audio/apple/AudioEngine-inl.mm @@ -192,12 +192,18 @@ static id s_AudioEngineSessionHandler = nullptr; AudioEngineImpl::AudioEngineImpl() : _lazyInitLoop(true) , _currentAudioID(0) +, _scheduler(nullptr) { } AudioEngineImpl::~AudioEngineImpl() { + if (_scheduler != nullptr) + { + _scheduler->unschedule(CC_SCHEDULE_SELECTOR(AudioEngineImpl::update), this); + } + if (s_ALContext) { alDeleteSources(MAX_AUDIOINSTANCES, _alSources); diff --git a/cocos/audio/win32/AudioEngine-win32.cpp b/cocos/audio/win32/AudioEngine-win32.cpp index 313695eff5..c50f4fa3ae 100644 --- a/cocos/audio/win32/AudioEngine-win32.cpp +++ b/cocos/audio/win32/AudioEngine-win32.cpp @@ -112,12 +112,18 @@ static ALCcontext *s_ALContext = nullptr; AudioEngineImpl::AudioEngineImpl() : _lazyInitLoop(true) , _currentAudioID(0) +, _scheduler(nullptr) { } AudioEngineImpl::~AudioEngineImpl() { + if (_scheduler != nullptr) + { + _scheduler->unschedule(CC_SCHEDULE_SELECTOR(AudioEngineImpl::update), this); + } + if (s_ALContext) { alDeleteSources(MAX_AUDIOINSTANCES, _alSources); diff --git a/cocos/audio/winrt/AudioEngine-winrt.cpp b/cocos/audio/winrt/AudioEngine-winrt.cpp index fc74a215d0..6649748470 100644 --- a/cocos/audio/winrt/AudioEngine-winrt.cpp +++ b/cocos/audio/winrt/AudioEngine-winrt.cpp @@ -38,6 +38,8 @@ AudioEngineImpl::AudioEngineImpl() AudioEngineImpl::~AudioEngineImpl() { + auto scheduler = cocos2d::Director::getInstance()->getScheduler(); + scheduler->unschedule(schedule_selector(AudioEngineImpl::update), this); _audioCaches.clear(); } diff --git a/tests/cpp-tests/Classes/NewAudioEngineTest/NewAudioEngineTest.cpp b/tests/cpp-tests/Classes/NewAudioEngineTest/NewAudioEngineTest.cpp index 91e910dab0..f17c4c806f 100644 --- a/tests/cpp-tests/Classes/NewAudioEngineTest/NewAudioEngineTest.cpp +++ b/tests/cpp-tests/Classes/NewAudioEngineTest/NewAudioEngineTest.cpp @@ -207,6 +207,12 @@ std::string AudioEngineTestDemo::title() const return "New Audio Engine Test"; } +void AudioEngineTestDemo::onBackCallback(cocos2d::Ref* sender) +{ + AudioEngine::end(); + TestCase::onBackCallback(sender); +} + // AudioControlTest bool AudioControlTest::init() { diff --git a/tests/cpp-tests/Classes/NewAudioEngineTest/NewAudioEngineTest.h b/tests/cpp-tests/Classes/NewAudioEngineTest/NewAudioEngineTest.h index ac32b69fd6..d575c2f1d0 100644 --- a/tests/cpp-tests/Classes/NewAudioEngineTest/NewAudioEngineTest.h +++ b/tests/cpp-tests/Classes/NewAudioEngineTest/NewAudioEngineTest.h @@ -41,6 +41,9 @@ public: virtual std::string title() const override; virtual void onExit() override; + + virtual void onBackCallback(cocos2d::Ref* sender) override; + protected: std::shared_ptr _isDestroyed; };