From 83fc6fd1a82cf624ad8885d59ddecf998d2319bb Mon Sep 17 00:00:00 2001 From: Wenhai Lin Date: Thu, 21 Apr 2016 15:19:47 +0800 Subject: [PATCH] Bug fix: 1.Fixed crash in win for rich text 2.Fixed crash when custom SSL certificate is set on iOS 3.Fixed SocketIO crash on reconnect 4.Fixed ActionManager memory leak 5.Fixed crash in MenuItem::activate 6.Fixed crash on exit game 7.Fixed crash in TableView::updateCellAtIndex 8.Fixed crash in RichText on window --- native/cocos/2d/CCActionManager.cpp | 5 + native/cocos/2d/CCMenuItem.cpp | 8 -- native/cocos/2d/CCNode.cpp | 2 +- native/cocos/base/CCDirector.cpp | 19 +-- .../cocos/network/HttpAsynConnection-apple.m | 9 +- native/cocos/network/SocketIO.cpp | 6 + native/cocos/network/WebSocket.cpp | 129 ++++++++---------- native/cocos/network/WebSocket.h | 2 +- .../js-bindings/manual/ScriptingCore.cpp | 1 + .../manual/network/jsb_websocket.cpp | 7 +- native/cocos/ui/UIRichText.cpp | 7 +- .../GUI/CCScrollView/CCTableView.cpp | 11 +- 12 files changed, 96 insertions(+), 110 deletions(-) diff --git a/native/cocos/2d/CCActionManager.cpp b/native/cocos/2d/CCActionManager.cpp index 43e2ea44f2..a588e320a7 100644 --- a/native/cocos/2d/CCActionManager.cpp +++ b/native/cocos/2d/CCActionManager.cpp @@ -446,6 +446,11 @@ void ActionManager::update(float dt) { deleteHashElement(_currentTarget); } + ////if some node refrence 'target', it's refrence count >= 2 (issues #14050) + else if(_currentTarget->target->getReferenceCount() == 1) + { + deleteHashElement(_currentTarget); + } } // issue #635 diff --git a/native/cocos/2d/CCMenuItem.cpp b/native/cocos/2d/CCMenuItem.cpp index 8fa6b7bce2..8a62642890 100644 --- a/native/cocos/2d/CCMenuItem.cpp +++ b/native/cocos/2d/CCMenuItem.cpp @@ -89,14 +89,6 @@ void MenuItem::activate() { _callback(this); } -#if CC_ENABLE_SCRIPT_BINDING - if (kScriptTypeNone != _scriptType && ScriptEngineManager::ShareInstance) - { - BasicScriptData data(this); - ScriptEvent scriptEvent(kMenuClickedEvent,&data); - ScriptEngineManager::ShareInstance->getScriptEngine()->sendEvent(&scriptEvent); - } -#endif } } diff --git a/native/cocos/2d/CCNode.cpp b/native/cocos/2d/CCNode.cpp index 18c93352e5..9abfe98ae5 100644 --- a/native/cocos/2d/CCNode.cpp +++ b/native/cocos/2d/CCNode.cpp @@ -189,7 +189,7 @@ Node::~Node() #endif if (_running) { - log("Node still marked as running on node destruction! Was base class onExit() called in derived class onExit() implementations?"); + CCLOG("Node still marked as running on node destruction! Was base class onExit() called in derived class onExit() implementations?"); } CC_SAFE_RELEASE(_eventDispatcher); } diff --git a/native/cocos/base/CCDirector.cpp b/native/cocos/base/CCDirector.cpp index 91969e4ca5..5b56f5d68c 100644 --- a/native/cocos/base/CCDirector.cpp +++ b/native/cocos/base/CCDirector.cpp @@ -183,17 +183,8 @@ Director::~Director() CC_SAFE_RELEASE(_runningScene); CC_SAFE_RELEASE(_notificationNode); - if (_scheduler) - { - delete _scheduler; - _scheduler = nullptr; - } - - if (_actionManager) - { - delete _actionManager; - _actionManager = nullptr; - } + CC_SAFE_RELEASE(_scheduler); + CC_SAFE_RELEASE(_actionManager); delete _eventBeforeUpdate; delete _eventAfterUpdate; @@ -206,11 +197,7 @@ Director::~Director() delete _console; - if (_eventDispatcher) - { - delete _eventDispatcher; - _eventDispatcher = nullptr; - } + CC_SAFE_RELEASE(_eventDispatcher); // delete _lastUpdate CC_SAFE_DELETE(_lastUpdate); diff --git a/native/cocos/network/HttpAsynConnection-apple.m b/native/cocos/network/HttpAsynConnection-apple.m index 6814005c31..6bb10002a7 100755 --- a/native/cocos/network/HttpAsynConnection-apple.m +++ b/native/cocos/network/HttpAsynConnection-apple.m @@ -187,11 +187,14 @@ CFDataRef errDataRef = SecTrustCopyExceptions(serverTrust); SecTrustSetExceptions(serverTrust, errDataRef); SecTrustEvaluate(serverTrust, &trustResult); - [(id)errDataRef release]; + CFRelease(errDataRef); } [certData release]; - [(id)certArrayRef release]; - [(id)certArrayRef release]; + if (cert) + CFRelease(cert); + if (certArrayRef) + CFRelease(certArrayRef); + //Did our custom trust chain evaluate successfully? return trustResult = kSecTrustResultUnspecified || trustResult == kSecTrustResultProceed; } diff --git a/native/cocos/network/SocketIO.cpp b/native/cocos/network/SocketIO.cpp index 07c17c3cf9..8901048ea7 100644 --- a/native/cocos/network/SocketIO.cpp +++ b/native/cocos/network/SocketIO.cpp @@ -986,6 +986,12 @@ void SIOClientImpl::onClose(WebSocket* ws) { iter->second->socketClosed(); } + // discard this client + _connected = false; + if (Director::DirectorInstance) + Director::DirectorInstance->getScheduler()->unscheduleAllForTarget(this); + + SocketIO::getInstance()->removeSocket(_uri); } this->release(); diff --git a/native/cocos/network/WebSocket.cpp b/native/cocos/network/WebSocket.cpp index 961f24a3c2..0d43df7938 100644 --- a/native/cocos/network/WebSocket.cpp +++ b/native/cocos/network/WebSocket.cpp @@ -122,8 +122,6 @@ public: // Creates a new thread bool createWebSocketThread(const WebSocket& ws); - // Quits websocket thread. - void quitWebSocketThread(); // Sends message to Cocos thread. It's needed to be invoked in Websocket thread. void sendMessageToCocosThread(const std::function& cb); @@ -141,7 +139,7 @@ private: std::mutex _subThreadWsMessageQueueMutex; std::thread* _subThreadInstance; WebSocket* _ws; - std::atomic _needQuit; + friend class WebSocket; }; @@ -166,11 +164,9 @@ public: } }; -// Implementation of WsThreadHelper WsThreadHelper::WsThreadHelper() : _subThreadInstance(nullptr) , _ws(nullptr) -, _needQuit(false) { _subThreadWsMessageQueue = new (std::nothrow) std::list(); } @@ -186,34 +182,14 @@ bool WsThreadHelper::createWebSocketThread(const WebSocket& ws) { _ws = const_cast(&ws); - // Creates websocket thread - _subThreadInstance = new (std::nothrow) std::thread(&WsThreadHelper::wsThreadEntryFunc, this); + _subThreadInstance = new (std::nothrow) std::thread(&WebSocket::onSubThreadLoop, _ws); return true; } -void WsThreadHelper::quitWebSocketThread() -{ - _needQuit = true; -} - -void WsThreadHelper::wsThreadEntryFunc() -{ - LOGD("WebSocket thread start, helper instance: %p\n", this); - _ws->onSubThreadStarted(); - - while (!_needQuit) - { - _ws->onSubThreadLoop(); - } - - _ws->onSubThreadEnded(); - - LOGD("WebSocket thread exit, helper instance: %p\n", this); -} - void WsThreadHelper::sendMessageToCocosThread(const std::function& cb) { - Director::getInstance()->getScheduler()->performFunctionInCocosThread(cb); + if (Director::DirectorInstance) + Director::DirectorInstance->getScheduler()->performFunctionInCocosThread(cb); } void WsThreadHelper::sendMessageToWebSocketThread(WsMessage *msg) @@ -230,7 +206,6 @@ void WsThreadHelper::joinWebSocketThread() } } -// Define a WebSocket frame class WebSocketFrame { public: @@ -281,8 +256,6 @@ private: ssize_t _frameLength; std::vector _data; }; -// - enum WS_MSG { WS_MSG_TO_SUBTRHEAD_SENDING_STRING = 0, @@ -295,14 +268,15 @@ void WebSocket::closeAllConnections() { if (__websocketInstances != nullptr) { + auto copyInstances = *__websocketInstances; ssize_t count = __websocketInstances->size(); for (ssize_t i = count-1; i >=0 ; i--) { - WebSocket* instance = __websocketInstances->at(i); + WebSocket* instance = copyInstances.at(i); instance->close(); } - - __websocketInstances->clear(); + + delete __websocketInstances; __websocketInstances = nullptr; } } @@ -340,6 +314,12 @@ WebSocket::WebSocket() WebSocket::~WebSocket() { + *_isDestroyed = true; + + if (Director::DirectorInstance) { + Director::DirectorInstance->getEventDispatcher()->removeEventListener(_resetDirectorListener); + } + LOGD("In the destructor of WebSocket (%p)\n", this); CC_SAFE_DELETE(_wsHelper); @@ -364,11 +344,6 @@ WebSocket::~WebSocket() LOGD("ERROR: WebSocket instance (%p) wasn't added to the container which saves websocket instances!\n", this); } } - - if (Director::DirectorInstance) { - Director::DirectorInstance->getEventDispatcher()->removeEventListener(_resetDirectorListener); - } - *_isDestroyed = true; } bool WebSocket::init(const Delegate& delegate, @@ -518,6 +493,7 @@ void WebSocket::close() _readStateMutex.unlock(); return; } + // Sets the state to 'closed' to make sure 'onConnectionClosed' which is // invoked by websocket thread don't post 'close' message to Cocos thread since // WebSocket instance is destroyed at next frame. @@ -525,9 +501,9 @@ void WebSocket::close() _readyState = State::CLOSED; _readStateMutex.unlock(); - _wsHelper->quitWebSocketThread(); LOGD("Waiting WebSocket (%p) to exit!\n", this); _wsHelper->joinWebSocketThread(); + // Since 'onConnectionClosed' didn't post message to Cocos Thread for invoking 'onClose' callback, do it here. // onClose must be invoked at the end of this method. _delegate->onClose(this); @@ -535,7 +511,10 @@ void WebSocket::close() void WebSocket::closeAsync() { - _wsHelper->quitWebSocketThread(); + _readStateMutex.lock(); + if (_readyState != State::CLOSING && _readyState != State::CLOSED) + _readyState = State::CLOSING; + _readStateMutex.unlock(); } WebSocket::State WebSocket::getReadyState() @@ -546,30 +525,38 @@ WebSocket::State WebSocket::getReadyState() void WebSocket::onSubThreadLoop() { - _readStateMutex.lock(); - if (_wsContext && _readyState != State::CLOSED && _readyState != State::CLOSING) - { - _readStateMutex.unlock(); - _wsHelper->_subThreadWsMessageQueueMutex.lock(); - bool isEmpty = _wsHelper->_subThreadWsMessageQueue->empty(); - _wsHelper->_subThreadWsMessageQueueMutex.unlock(); - if (!isEmpty) + onSubThreadStarted(); + + while (true) { + _readStateMutex.lock(); + if (_wsContext && (_readyState == State::CONNECTING || _readyState == State::OPEN)) { - lws_callback_on_writable(_wsInstance); + _readStateMutex.unlock(); + _wsHelper->_subThreadWsMessageQueueMutex.lock(); + bool isEmpty = _wsHelper->_subThreadWsMessageQueue->empty(); + _wsHelper->_subThreadWsMessageQueueMutex.unlock(); + if (!isEmpty) + { + lws_callback_on_writable(_wsInstance); + } + + lws_service(_wsContext, 50); + } + else + { + LOGD("Ready state is closing or was closed, code=%d, quit websocket thread!\n", _readyState); + _readStateMutex.unlock(); + break; } - - lws_service(_wsContext, 50); - } - else - { - LOGD("Ready state is closing or was closed, code=%d, quit websocket thread!\n", _readyState); - _readStateMutex.unlock(); - _wsHelper->quitWebSocketThread(); } + + onSubThreadEnded(); } void WebSocket::onSubThreadStarted() { + LOGD("WebSocket::onSubThreadStarted, instance: %p\n", this); + static const struct lws_extension exts[] = { { "permessage-deflate", @@ -603,7 +590,7 @@ void WebSocket::onSubThreadStarted() info.options = 0; info.user = this; - int log_level = LLL_ERR | LLL_WARN | LLL_NOTICE/* | LLL_INFO | LLL_DEBUG | LLL_PARSER*/ | LLL_HEADER | LLL_EXT | LLL_CLIENT | LLL_LATENCY; + int log_level = LLL_ERR | LLL_WARN | LLL_NOTICE | LLL_HEADER | LLL_EXT | LLL_CLIENT | LLL_LATENCY; lws_set_log_level(log_level, printWebSocketLog); _wsContext = lws_create_context(&info); @@ -625,10 +612,11 @@ void WebSocket::onSubThreadStarted() char portStr[10]; sprintf(portStr, "%d", _port); std::string ads_port = _host + ":" + portStr; - - _wsInstance = lws_client_connect(_wsContext, _host.c_str(), _port, _SSLConnection, - _path.c_str(), ads_port.c_str(), ads_port.c_str(), - name.c_str(), -1); + + lws_client_connect_info cInfo = {_wsContext, _host.c_str(), (int)_port, _SSLConnection, + _path.c_str(), ads_port.c_str(), ads_port.c_str(), + name.c_str(), -1,nullptr,nullptr}; + _wsInstance = lws_client_connect_via_info(&cInfo); if (nullptr == _wsInstance) { @@ -646,7 +634,10 @@ void WebSocket::onSubThreadEnded() if (_wsContext != nullptr) { lws_context_destroy(_wsContext); + _wsContext = nullptr; } + + LOGD("WebSocket::onSubThreadEnded, instance: %p\n", this); } void WebSocket::onClientWritable() @@ -801,7 +792,6 @@ void WebSocket::onClientReceivedData(void* in, ssize_t len) // If no more data pending, send it to the client thread size_t remainingSize = lws_remaining_packet_payload(_wsInstance); int isFinalFragment = lws_is_final_fragment(_wsInstance); -// LOGD("remainingSize: %d, isFinalFragment: %d\n", (int)remainingSize, isFinalFragment); if (remainingSize == 0 && isFinalFragment) { @@ -896,8 +886,7 @@ void WebSocket::onConnectionClosed() LOGD("WebSocket (%p) onConnectionClosed ...\n", this); _readyState = State::CLOSED; _readStateMutex.unlock(); - - _wsHelper->quitWebSocketThread(); + auto isDestroyed = _isDestroyed; _wsHelper->sendMessageToCocosThread([this, isDestroyed](){ if (*isDestroyed) { @@ -914,8 +903,6 @@ int WebSocket::onSocketCallback(struct lws *wsi, int reason, void *user, void *in, ssize_t len) { - //LOGD("socket callback for %d reason\n", reason); - switch (reason) { case LWS_CALLBACK_CLIENT_ESTABLISHED: @@ -938,9 +925,11 @@ int WebSocket::onSocketCallback(struct lws *wsi, case LWS_CALLBACK_CLIENT_WRITEABLE: onClientWritable(); break; - + + case LWS_CALLBACK_GET_THREAD_ID: + break; default: -// LOGD("Unhandled websocket event: %d\n", reason); + //LOGD("Unhandled websocket event: %d\n", reason); break; } diff --git a/native/cocos/network/WebSocket.h b/native/cocos/network/WebSocket.h index b8235a8ca1..f9d79244c6 100644 --- a/native/cocos/network/WebSocket.h +++ b/native/cocos/network/WebSocket.h @@ -227,7 +227,7 @@ private: std::mutex _readStateMutex; State _readyState; std::string _host; - unsigned int _port; + int _port; std::string _path; std::vector _receivedData; diff --git a/native/cocos/scripting/js-bindings/manual/ScriptingCore.cpp b/native/cocos/scripting/js-bindings/manual/ScriptingCore.cpp index 9db5fa4073..26f90305d4 100644 --- a/native/cocos/scripting/js-bindings/manual/ScriptingCore.cpp +++ b/native/cocos/scripting/js-bindings/manual/ScriptingCore.cpp @@ -704,6 +704,7 @@ JS::PersistentRootedScript* ScriptingCore::compileScript(const std::string& path if (compileSucceed) { return script; } else { + cocos2d::log("ScriptingCore:: compileScript fail:%s", path.c_str()); CC_SAFE_DELETE(script); return nullptr; } diff --git a/native/cocos/scripting/js-bindings/manual/network/jsb_websocket.cpp b/native/cocos/scripting/js-bindings/manual/network/jsb_websocket.cpp index a8e379345f..77d2ace2bb 100644 --- a/native/cocos/scripting/js-bindings/manual/network/jsb_websocket.cpp +++ b/native/cocos/scripting/js-bindings/manual/network/jsb_websocket.cpp @@ -175,7 +175,7 @@ public: js_proxy_t * p = jsb_get_native_proxy(ws); if (!p) return; - if (Director::DirectorInstance == nullptr || ScriptEngineManager::ShareInstance == nullptr) + if (Director::DirectorInstance == nullptr || ScriptEngineManager::ShareInstance == nullptr || Director::DirectorInstance->getRunningScene() == nullptr) return; JSB_AUTOCOMPARTMENT_WITH_GLOBAL_OBJCET @@ -283,7 +283,6 @@ bool js_cocos2dx_extension_WebSocket_constructor(JSContext *cx, uint32_t argc, j if (argc == 1 || argc == 2) { - std::string url; do { @@ -377,8 +376,8 @@ static bool js_cocos2dx_extension_WebSocket_get_readyState(JSContext *cx, uint32 } } -void register_jsb_websocket(JSContext *cx, JS::HandleObject global) { - +void register_jsb_websocket(JSContext *cx, JS::HandleObject global) +{ js_cocos2dx_websocket_class = (JSClass *)calloc(1, sizeof(JSClass)); js_cocos2dx_websocket_class->name = "WebSocket"; js_cocos2dx_websocket_class->addProperty = JS_PropertyStub; diff --git a/native/cocos/ui/UIRichText.cpp b/native/cocos/ui/UIRichText.cpp index 30e4164215..e985e95732 100644 --- a/native/cocos/ui/UIRichText.cpp +++ b/native/cocos/ui/UIRichText.cpp @@ -25,6 +25,7 @@ #include "UIRichText.h" #include +#include #include "tinyxml2/tinyxml2.h" #include "platform/CCFileUtils.h" @@ -705,7 +706,7 @@ static int getPrevWord(const std::string& text, int idx) // start from idx-1 for (int i=idx-1; i>=0; --i) { - if (!std::isalnum(text[i])) + if (!std::isalnum(text[i], std::locale())) return i; } return -1; @@ -715,7 +716,7 @@ static bool isWrappable(const std::string& text) { for (int i=0; i 0) diff --git a/native/extensions/GUI/CCScrollView/CCTableView.cpp b/native/extensions/GUI/CCScrollView/CCTableView.cpp index be25b65319..5cdd81f860 100644 --- a/native/extensions/GUI/CCScrollView/CCTableView.cpp +++ b/native/extensions/GUI/CCScrollView/CCTableView.cpp @@ -155,14 +155,17 @@ void TableView::updateCellAtIndex(ssize_t idx) return; } - TableViewCell* cell = this->cellAtIndex(idx); + TableViewCell* cell = cellAtIndex(idx); if (cell) { - this->_moveCellOutOfSight(cell); + _moveCellOutOfSight(cell); } cell = _dataSource->tableCellAtIndex(this, idx); - this->_setIndexForCell(idx, cell); - this->_addCellIfNecessary(cell); + if (cell) + { + _setIndexForCell(idx, cell); + _addCellIfNecessary(cell); + } } void TableView::insertCellAtIndex(ssize_t idx)