From dfa0360d99a48293cfb79d4dd663c40a490596d2 Mon Sep 17 00:00:00 2001 From: saumyamukul Date: Mon, 6 Aug 2018 00:17:08 -0600 Subject: [PATCH] Fix memory leaks (#18564) --- cocos/network/WebSocket.cpp | 6 ++++++ cocos/network/WebSocket.h | 1 + 2 files changed, 7 insertions(+) diff --git a/cocos/network/WebSocket.cpp b/cocos/network/WebSocket.cpp index 6e8568a1eb..c9c45a7fe2 100644 --- a/cocos/network/WebSocket.cpp +++ b/cocos/network/WebSocket.cpp @@ -570,6 +570,11 @@ WebSocket::~WebSocket() CC_SAFE_DELETE(__wsHelper); } + for(auto name:_protocolNames){ + free(name); + } + free(_lwsProtocols); + Director::getInstance()->getEventDispatcher()->removeEventListener(_resetDirectorListener); *_isDestroyed = true; @@ -601,6 +606,7 @@ bool WebSocket::init(const Delegate& delegate, _lwsProtocols[i].callback = WebSocketCallbackWrapper::onSocketCallback; size_t nameLen = protocols->at(i).length(); char* name = (char*)malloc(nameLen + 1); + _protocolNames.push_back(name); name[nameLen] = '\0'; strcpy(name, protocols->at(i).c_str()); _lwsProtocols[i].name = name; diff --git a/cocos/network/WebSocket.h b/cocos/network/WebSocket.h index e70d26a370..f939944d00 100644 --- a/cocos/network/WebSocket.h +++ b/cocos/network/WebSocket.h @@ -255,6 +255,7 @@ private: std::mutex _closeMutex; std::condition_variable _closeCondition; + std::vector _protocolNames; enum class CloseState {