From d88d4ffeaddc3a2eb08c105d01a2e7b912e36688 Mon Sep 17 00:00:00 2001 From: Arnold <40414978+PatriceJiang@users.noreply.github.com> Date: Thu, 25 Apr 2019 18:43:01 +0800 Subject: [PATCH] Use smart pointers to hold references of global Application::_scheduler (#1732) * use std::weak_ptr * revert build/libcocos2d.vcxproj * fix mac & ios & android * declare variable in if block * revert build/libcocos2d.vcxproj --- native/cocos/audio/apple/AudioEngine-inl.h | 2 +- native/cocos/audio/apple/AudioEngine-inl.mm | 26 ++++++++++------- .../cocos/audio/win32/AudioEngine-win32.cpp | 28 ++++++++++++------- native/cocos/audio/win32/AudioEngine-win32.h | 2 +- native/cocos/network/CCDownloader-curl.cpp | 21 +++++++++++--- native/cocos/network/CCDownloader-curl.h | 4 +-- native/cocos/network/HttpClient-android.cpp | 14 ++++++---- native/cocos/network/HttpClient-apple.mm | 15 ++++++---- native/cocos/network/HttpClient.cpp | 16 +++++++---- native/cocos/network/HttpClient.h | 2 +- native/cocos/platform/CCApplication.h | 2 +- .../cocos/platform/ios/CCApplication-ios.mm | 2 +- 12 files changed, 85 insertions(+), 49 deletions(-) diff --git a/native/cocos/audio/apple/AudioEngine-inl.h b/native/cocos/audio/apple/AudioEngine-inl.h index 8a0500cac2..a7d7ec395f 100644 --- a/native/cocos/audio/apple/AudioEngine-inl.h +++ b/native/cocos/audio/apple/AudioEngine-inl.h @@ -86,7 +86,7 @@ private: bool _lazyInitLoop; int _currentAudioID; - Scheduler* _scheduler; + std::weak_ptr _scheduler; }; NS_CC_END #endif // __AUDIO_ENGINE_INL_H_ diff --git a/native/cocos/audio/apple/AudioEngine-inl.mm b/native/cocos/audio/apple/AudioEngine-inl.mm index 3884e97e30..875c51379b 100644 --- a/native/cocos/audio/apple/AudioEngine-inl.mm +++ b/native/cocos/audio/apple/AudioEngine-inl.mm @@ -236,16 +236,15 @@ ALvoid AudioEngineImpl::myAlSourceNotificationCallback(ALuint sid, ALuint notifi AudioEngineImpl::AudioEngineImpl() : _lazyInitLoop(true) , _currentAudioID(0) -, _scheduler(nullptr) { s_instance = this; } AudioEngineImpl::~AudioEngineImpl() { - if (_scheduler != nullptr) + if (auto sche = _scheduler.lock()) { - _scheduler->unschedule("AudioEngine", this); + sche->unschedule("AudioEngine", this); } if (s_ALContext) { @@ -430,7 +429,10 @@ int AudioEngineImpl::play2d(const std::string &filePath ,bool loop ,float volume if (_lazyInitLoop) { _lazyInitLoop = false; - _scheduler->schedule(CC_CALLBACK_1(AudioEngineImpl::update, this), this, 0.05f, false, "AudioEngine"); + if(auto sche = _scheduler.lock()) + { + sche->schedule(CC_CALLBACK_1(AudioEngineImpl::update, this), this, 0.05f, false, "AudioEngine"); + } } return _currentAudioID++; @@ -444,12 +446,14 @@ void AudioEngineImpl::_play2d(AudioCache *cache, int audioID) _threadMutex.lock(); auto playerIt = _audioPlayers.find(audioID); if (playerIt != _audioPlayers.end() && playerIt->second->play2d()) { - _scheduler->performFunctionInCocosThread([audioID](){ + if(auto sche = _scheduler.lock()){ + sche->performFunctionInCocosThread([audioID](){ - if (AudioEngine::_audioIDInfoMap.find(audioID) != AudioEngine::_audioIDInfoMap.end()) { - AudioEngine::_audioIDInfoMap[audioID].state = AudioEngine::AudioState::PLAYING; - } - }); + if (AudioEngine::_audioIDInfoMap.find(audioID) != AudioEngine::_audioIDInfoMap.end()) { + AudioEngine::_audioIDInfoMap[audioID].state = AudioEngine::AudioState::PLAYING; + } + }); + } } _threadMutex.unlock(); } @@ -695,7 +699,9 @@ void AudioEngineImpl::update(float dt) if(_audioPlayers.empty()){ _lazyInitLoop = true; - _scheduler->unschedule("AudioEngine", this); + if(auto sche = _scheduler.lock()) { + sche->unschedule("AudioEngine", this); + } } } diff --git a/native/cocos/audio/win32/AudioEngine-win32.cpp b/native/cocos/audio/win32/AudioEngine-win32.cpp index d49fd7ed23..bf81c8c3f5 100644 --- a/native/cocos/audio/win32/AudioEngine-win32.cpp +++ b/native/cocos/audio/win32/AudioEngine-win32.cpp @@ -112,16 +112,15 @@ static ALCcontext *s_ALContext = nullptr; AudioEngineImpl::AudioEngineImpl() : _lazyInitLoop(true) , _currentAudioID(0) -, _scheduler(nullptr) { } AudioEngineImpl::~AudioEngineImpl() { - if (_scheduler != nullptr) + if (auto sche = _scheduler.lock()) { - _scheduler->unschedule("AudioEngine", this); + sche->unschedule("AudioEngine", this); } if (s_ALContext) { @@ -251,7 +250,10 @@ int AudioEngineImpl::play2d(const std::string &filePath ,bool loop ,float volume if (_lazyInitLoop) { _lazyInitLoop = false; - _scheduler->schedule(CC_CALLBACK_1(AudioEngineImpl::update, this), this, 0.05f, false, "AudioEngine"); + if(auto sche = _scheduler.lock()) + { + sche->schedule(CC_CALLBACK_1(AudioEngineImpl::update, this), this, 0.05f, false, "AudioEngine"); + } } return _currentAudioID++; @@ -265,12 +267,15 @@ void AudioEngineImpl::_play2d(AudioCache *cache, int audioID) _threadMutex.lock(); auto playerIt = _audioPlayers.find(audioID); if (playerIt != _audioPlayers.end() && playerIt->second->play2d()) { - _scheduler->performFunctionInCocosThread([audioID](){ + if (auto sche = _scheduler.lock()) + { + sche->performFunctionInCocosThread([audioID]() { - if (AudioEngine::_audioIDInfoMap.find(audioID) != AudioEngine::_audioIDInfoMap.end()) { - AudioEngine::_audioIDInfoMap[audioID].state = AudioEngine::AudioState::PLAYING; - } - }); + if (AudioEngine::_audioIDInfoMap.find(audioID) != AudioEngine::_audioIDInfoMap.end()) { + AudioEngine::_audioIDInfoMap[audioID].state = AudioEngine::AudioState::PLAYING; + } + }); + } } _threadMutex.unlock(); } @@ -513,7 +518,10 @@ void AudioEngineImpl::update(float dt) if(_audioPlayers.empty()){ _lazyInitLoop = true; - _scheduler->unschedule("AudioEngine", this); + if(auto sche = _scheduler.lock()) + { + sche->unschedule("AudioEngine", this); + } } } diff --git a/native/cocos/audio/win32/AudioEngine-win32.h b/native/cocos/audio/win32/AudioEngine-win32.h index a2faf04351..7e832c633a 100644 --- a/native/cocos/audio/win32/AudioEngine-win32.h +++ b/native/cocos/audio/win32/AudioEngine-win32.h @@ -84,7 +84,7 @@ private: bool _lazyInitLoop; int _currentAudioID; - Scheduler* _scheduler; + std::weak_ptr _scheduler; }; NS_CC_END #endif // __AUDIO_ENGINE_INL_H_ diff --git a/native/cocos/network/CCDownloader-curl.cpp b/native/cocos/network/CCDownloader-curl.cpp index b139160731..3b0e730448 100644 --- a/native/cocos/network/CCDownloader-curl.cpp +++ b/native/cocos/network/CCDownloader-curl.cpp @@ -729,16 +729,22 @@ namespace cocos2d { namespace network { sprintf(key, "DownloaderCURL(%p)", this); _schedulerKey = key; - _scheduler->schedule(bind(&DownloaderCURL::_onSchedule, this, placeholders::_1), + if(auto sche = _scheduler.lock()) + { + sche->schedule(bind(&DownloaderCURL::_onSchedule, this, placeholders::_1), this, 0.1f, true, _schedulerKey); + } } DownloaderCURL::~DownloaderCURL() { - _scheduler->unschedule(_schedulerKey, this); + if(auto sche = _scheduler.lock()) + { + sche->unschedule(_schedulerKey, this); + } _impl->stop(); DLLOG("Destruct DownloaderCURL %p", this); @@ -753,7 +759,11 @@ namespace cocos2d { namespace network { _impl->addTask(task, coTask); _impl->run(); - _scheduler->resumeTarget(this); + + if(auto sche = _scheduler.lock()) + { + sche->resumeTarget(this); + } return coTask; } @@ -793,7 +803,10 @@ namespace cocos2d { namespace network { _impl->getFinishedTasks(tasks); if (_impl->stoped()) { - _scheduler->pauseTarget(this); + if (auto sche = _scheduler.lock()) + { + sche->pauseTarget(this); + } } for (auto& wrapper : tasks) diff --git a/native/cocos/network/CCDownloader-curl.h b/native/cocos/network/CCDownloader-curl.h index 222ab25d76..3b010050b6 100644 --- a/native/cocos/network/CCDownloader-curl.h +++ b/native/cocos/network/CCDownloader-curl.h @@ -56,8 +56,8 @@ namespace cocos2d { namespace network // scheduler for update processing and finished task in main schedule void _onSchedule(float); - std::string _schedulerKey; - Scheduler* _scheduler; + std::string _schedulerKey; + std::weak_ptr _scheduler; }; }} // namespace cocos2d::network diff --git a/native/cocos/network/HttpClient-android.cpp b/native/cocos/network/HttpClient-android.cpp index da8c06f10f..2fe9b39753 100644 --- a/native/cocos/network/HttpClient-android.cpp +++ b/native/cocos/network/HttpClient-android.cpp @@ -838,9 +838,9 @@ void HttpClient::networkThread() _responseQueueMutex.unlock(); _schedulerMutex.lock(); - if (nullptr != _scheduler) + if (auto sche = _scheduler.lock()) { - _scheduler->performFunctionInCocosThread(CC_CALLBACK_0(HttpClient::dispatchResponseCallbacks, this)); + sche->performFunctionInCocosThread(CC_CALLBACK_0(HttpClient::dispatchResponseCallbacks, this)); } _schedulerMutex.unlock(); } @@ -866,9 +866,9 @@ void HttpClient::networkThreadAlone(HttpRequest* request, HttpResponse* response processResponse(response, responseMessage); _schedulerMutex.lock(); - if (_scheduler != nullptr) + if (auto sche = _scheduler.lock()) { - _scheduler->performFunctionInCocosThread([this, response, request]{ + sche->performFunctionInCocosThread([this, response, request]{ const ccHttpRequestCallback& callback = request->getResponseCallback(); if (callback != nullptr) @@ -909,10 +909,12 @@ void HttpClient::destroyInstance() auto thiz = _httpClient; _httpClient = nullptr; - thiz->_scheduler->unscheduleAllForTarget(thiz); + if(auto sche = thiz->_scheduler.lock()) { + sche->unscheduleAllForTarget(thiz); + } thiz->_schedulerMutex.lock(); - thiz->_scheduler = nullptr; + thiz->_scheduler.reset(); thiz->_schedulerMutex.unlock(); { diff --git a/native/cocos/network/HttpClient-apple.mm b/native/cocos/network/HttpClient-apple.mm index f69884365b..6263313f23 100644 --- a/native/cocos/network/HttpClient-apple.mm +++ b/native/cocos/network/HttpClient-apple.mm @@ -76,9 +76,9 @@ void HttpClient::networkThread() _responseQueueMutex.unlock(); _schedulerMutex.lock(); - if (nullptr != _scheduler) + if (auto sche = _scheduler.lock()) { - _scheduler->performFunctionInCocosThread(CC_CALLBACK_0(HttpClient::dispatchResponseCallbacks, this)); + sche->performFunctionInCocosThread(CC_CALLBACK_0(HttpClient::dispatchResponseCallbacks, this)); } _schedulerMutex.unlock(); } @@ -104,9 +104,9 @@ void HttpClient::networkThreadAlone(HttpRequest* request, HttpResponse* response processResponse(response, responseMessage); _schedulerMutex.lock(); - if (nullptr != _scheduler) + if (auto sche = _scheduler.lock()) { - _scheduler->performFunctionInCocosThread([this, response, request]{ + sche->performFunctionInCocosThread([this, response, request]{ const ccHttpRequestCallback& callback = request->getResponseCallback(); if (callback != nullptr) @@ -312,9 +312,12 @@ void HttpClient::destroyInstance() auto thiz = _httpClient; _httpClient = nullptr; - thiz->_scheduler->unscheduleAllForTarget(thiz); + if(auto sche = thiz->_scheduler.lock()) + { + sche->unscheduleAllForTarget(thiz); + } thiz->_schedulerMutex.lock(); - thiz->_scheduler = nullptr; + thiz->_scheduler.reset(); thiz->_schedulerMutex.unlock(); thiz->_requestQueueMutex.lock(); diff --git a/native/cocos/network/HttpClient.cpp b/native/cocos/network/HttpClient.cpp index f9a64c5757..bbf65a71ca 100644 --- a/native/cocos/network/HttpClient.cpp +++ b/native/cocos/network/HttpClient.cpp @@ -115,9 +115,9 @@ void HttpClient::networkThread() _responseQueueMutex.unlock(); _schedulerMutex.lock(); - if (nullptr != _scheduler) + if (auto sche = _scheduler.lock()) { - _scheduler->performFunctionInCocosThread(CC_CALLBACK_0(HttpClient::dispatchResponseCallbacks, this)); + sche->performFunctionInCocosThread(CC_CALLBACK_0(HttpClient::dispatchResponseCallbacks, this)); } _schedulerMutex.unlock(); } @@ -143,9 +143,9 @@ void HttpClient::networkThreadAlone(HttpRequest* request, HttpResponse* response processResponse(response, responseMessage); _schedulerMutex.lock(); - if (nullptr != _scheduler) + if (auto sche = _scheduler.lock()) { - _scheduler->performFunctionInCocosThread([this, response, request]{ + sche->performFunctionInCocosThread([this, response, request]{ const ccHttpRequestCallback& callback = request->getResponseCallback(); if (callback != nullptr) @@ -356,9 +356,13 @@ void HttpClient::destroyInstance() auto thiz = _httpClient; _httpClient = nullptr; - thiz->_scheduler->unscheduleAllForTarget(thiz); + if(auto sche = thiz->_scheduler.lock()) + { + sche->unscheduleAllForTarget(thiz); + } + thiz->_schedulerMutex.lock(); - thiz->_scheduler = nullptr; + thiz->_scheduler.reset(); thiz->_schedulerMutex.unlock(); thiz->_requestQueueMutex.lock(); diff --git a/native/cocos/network/HttpClient.h b/native/cocos/network/HttpClient.h index e8f0ee80ff..2be87e9749 100644 --- a/native/cocos/network/HttpClient.h +++ b/native/cocos/network/HttpClient.h @@ -185,7 +185,7 @@ private: int _threadCount; std::mutex _threadCountMutex; - Scheduler* _scheduler; + std::weak_ptr _scheduler; std::mutex _schedulerMutex; Vector _requestQueue; diff --git a/native/cocos/platform/CCApplication.h b/native/cocos/platform/CCApplication.h index f0d6f96d94..a517294eda 100644 --- a/native/cocos/platform/CCApplication.h +++ b/native/cocos/platform/CCApplication.h @@ -109,7 +109,7 @@ public: virtual void applicationWillEnterForeground(); inline void* getView() const { return _view; } - inline Scheduler* getScheduler() const { return _scheduler.get(); } + inline std::shared_ptr getScheduler() const { return _scheduler; } inline RenderTexture* getRenderTexture() const { return _renderTexture; } void runOnMainThread(); diff --git a/native/cocos/platform/ios/CCApplication-ios.mm b/native/cocos/platform/ios/CCApplication-ios.mm index fda6544ce4..3b97e2f741 100644 --- a/native/cocos/platform/ios/CCApplication-ios.mm +++ b/native/cocos/platform/ios/CCApplication-ios.mm @@ -72,7 +72,7 @@ namespace float _systemVersion; BOOL _isAppActive; cocos2d::Application* _application; - cocos2d::Scheduler* _scheduler; + std::shared_ptr _scheduler; } -(void) startMainLoop; -(void) stopMainLoop;