fix asan reported errors (#20332)

This commit is contained in:
Arnold 2019-11-15 13:39:23 +08:00 committed by minggo
parent 49bca674cc
commit 587bcabf44
4 changed files with 83 additions and 67 deletions

View File

@ -694,7 +694,7 @@ protected:
PolygonInfo _polyInfo;
// opacity and RGB protocol
bool _opacityModifyRGB;
bool _opacityModifyRGB = false;
// image is flipped
bool _flippedX = false; /// Whether the sprite is flipped horizontally or not

View File

@ -32,6 +32,7 @@
#include "network/Uri.h"
#include <algorithm>
#include <sstream>
#include <memory>
#include <iterator>
#include "base/ccUTF8.h"
#include "base/CCDirector.h"
@ -79,8 +80,8 @@ public:
std::vector<std::string> getData()const{ return _args; };
virtual std::string stringify()const;
static SocketIOPacket * createPacketWithType(const std::string& type, SocketIOVersion version);
static SocketIOPacket * createPacketWithTypeIndex(int type, SocketIOVersion version);
static std::shared_ptr<SocketIOPacket> createPacketWithType(const std::string& type, SocketIOVersion version);
static std::shared_ptr<SocketIOPacket> createPacketWithTypeIndex(int type, SocketIOVersion version);
protected:
std::string _pId;//id message
std::string _ack;//
@ -305,37 +306,38 @@ SocketIOPacketV10x::~SocketIOPacketV10x()
_endpoint = "";
}
SocketIOPacket * SocketIOPacket::createPacketWithType(const std::string& type, SocketIOPacket::SocketIOVersion version)
std::shared_ptr<SocketIOPacket> SocketIOPacket::createPacketWithType(const std::string& type, SocketIOPacket::SocketIOVersion version)
{
SocketIOPacket *ret;
switch (version)
if(version == SocketIOPacket::SocketIOVersion::V09x)
{
case SocketIOPacket::SocketIOVersion::V09x:
ret = new (std::nothrow) SocketIOPacket;
break;
case SocketIOPacket::SocketIOVersion::V10x:
ret = new (std::nothrow) SocketIOPacketV10x;
break;
auto ret = std::make_shared<SocketIOPacket>();
ret->initWithType(type);
return ret;
}
ret->initWithType(type);
return ret;
else if(version == SocketIOPacket::SocketIOVersion::V10x)
{
auto ret = std::make_shared<SocketIOPacketV10x>();
ret->initWithType(type);
return ret;
}
return nullptr;
}
SocketIOPacket * SocketIOPacket::createPacketWithTypeIndex(int type, SocketIOPacket::SocketIOVersion version)
std::shared_ptr<SocketIOPacket> SocketIOPacket::createPacketWithTypeIndex(int type, SocketIOPacket::SocketIOVersion version)
{
SocketIOPacket *ret;
switch (version)
if(version == SocketIOPacket::SocketIOVersion::V09x)
{
case SocketIOPacket::SocketIOVersion::V09x:
ret = new (std::nothrow) SocketIOPacket;
break;
case SocketIOPacket::SocketIOVersion::V10x:
return new (std::nothrow) SocketIOPacketV10x;
break;
auto ret = std::make_shared<SocketIOPacket>();
ret->initWithTypeIndex(type);
return ret;
}
ret->initWithTypeIndex(type);
return ret;
else if(version == SocketIOPacket::SocketIOVersion::V10x)
{
auto ret = std::make_shared<SocketIOPacketV10x>();
ret->initWithTypeIndex(type);
return ret;
}
return nullptr;
}
/**
@ -343,8 +345,8 @@ SocketIOPacket * SocketIOPacket::createPacketWithTypeIndex(int type, SocketIOPac
* Clients/endpoints may share the same impl to accomplish multiplexing on the same websocket
*/
class SIOClientImpl :
public cocos2d::Ref,
public WebSocket::Delegate
public WebSocket::Delegate,
public std::enable_shared_from_this<SIOClientImpl>
{
private:
int _heartbeat, _timeout;
@ -362,7 +364,7 @@ public:
SIOClientImpl(const Uri& uri, const std::string& caFilePath);
virtual ~SIOClientImpl();
static SIOClientImpl* create(const Uri& uri, const std::string& caFilePath);
static std::shared_ptr<SIOClientImpl> create(const Uri& uri, const std::string& caFilePath);
virtual void onOpen(WebSocket* ws);
virtual void onMessage(WebSocket* ws, const WebSocket::Data& data);
@ -385,7 +387,7 @@ public:
void send(const std::string& endpoint, const std::string& s);
void send(const std::string& endpoint, const std::vector<std::string>& s);
void send(SocketIOPacket *packet);
void send(std::shared_ptr<SocketIOPacket>& packet);
void emit(const std::string& endpoint, const std::string& eventname, const std::string& args);
void emit(const std::string& endpoint, const std::string& eventname, const std::vector<std::string>& args);
@ -429,7 +431,14 @@ void SIOClientImpl::handshake()
request->setUrl(pre.str());
request->setRequestType(HttpRequest::Type::GET);
request->setResponseCallback(CC_CALLBACK_2(SIOClientImpl::handshakeResponse, this));
std::weak_ptr<SIOClientImpl> self = shared_from_this();
auto callback = [self](HttpClient* client, HttpResponse *resp) {
auto conn = self.lock();
if (conn) {
conn->handshakeResponse(client, resp);
}
};
request->setResponseCallback(callback);
request->setTag("handshake");
CCLOGINFO("SIOClientImpl::handshake() waiting");
@ -633,13 +642,13 @@ void SIOClientImpl::disconnect()
_ws->close();
}
SIOClientImpl* SIOClientImpl::create(const Uri& uri, const std::string& caFilePath)
std::shared_ptr<SIOClientImpl> SIOClientImpl::create(const Uri& uri, const std::string& caFilePath)
{
SIOClientImpl *s = new (std::nothrow) SIOClientImpl(uri, caFilePath);
if (s && s->init())
{
return s;
return std::shared_ptr<SIOClientImpl>(s);
}
return nullptr;
@ -657,7 +666,7 @@ void SIOClientImpl::addClient(const std::string& endpoint, SIOClient* client)
void SIOClientImpl::connectToEndpoint(const std::string& endpoint)
{
SocketIOPacket *packet = SocketIOPacket::createPacketWithType("connect", _version);
auto packet = SocketIOPacket::createPacketWithType("connect", _version);
packet->setEndpoint(endpoint);
this->send(packet);
}
@ -685,7 +694,7 @@ void SIOClientImpl::disconnectFromEndpoint(const std::string& endpoint)
void SIOClientImpl::heartbeat(float /*dt*/)
{
SocketIOPacket *packet = SocketIOPacket::createPacketWithType("heartbeat", _version);
auto packet = SocketIOPacket::createPacketWithType("heartbeat", _version);
this->send(packet);
@ -698,7 +707,7 @@ void SIOClientImpl::send(const std::string& endpoint, const std::vector<std::str
switch (_version) {
case SocketIOPacket::SocketIOVersion::V09x:
{
SocketIOPacket *packet = SocketIOPacket::createPacketWithType("message", _version);
auto packet = SocketIOPacket::createPacketWithType("message", _version);
packet->setEndpoint(endpoint);
for(auto &i : s)
{
@ -721,7 +730,7 @@ void SIOClientImpl::send(const std::string& endpoint, const std::string& s)
send(endpoint, t);
}
void SIOClientImpl::send(SocketIOPacket *packet)
void SIOClientImpl::send(std::shared_ptr<SocketIOPacket>& packet)
{
std::string req = packet->toString();
if (_connected)
@ -736,7 +745,7 @@ void SIOClientImpl::send(SocketIOPacket *packet)
void SIOClientImpl::emit(const std::string& endpoint, const std::string& eventname, const std::string& args)
{
CCLOGINFO("Emitting event \"%s\"", eventname.c_str());
SocketIOPacket *packet = SocketIOPacket::createPacketWithType("event", _version);
auto packet = SocketIOPacket::createPacketWithType("event", _version);
packet->setEndpoint(endpoint == "/" ? "" : endpoint);
packet->setEvent(eventname);
packet->addData(args);
@ -746,7 +755,7 @@ void SIOClientImpl::emit(const std::string& endpoint, const std::string& eventna
void SIOClientImpl::emit(const std::string& endpoint, const std::string& eventname, const std::vector<std::string>& args)
{
CCLOGINFO("Emitting event \"%s\"", eventname.c_str());
SocketIOPacket *packet = SocketIOPacket::createPacketWithType("event", _version);
auto packet = SocketIOPacket::createPacketWithType("event", _version);
packet->setEndpoint(endpoint == "/" ? "" : endpoint);
packet->setEvent(eventname);
for (auto &arg : args) {
@ -759,7 +768,9 @@ void SIOClientImpl::onOpen(WebSocket* /*ws*/)
{
_connected = true;
SocketIO::getInstance()->addSocket(_uri.getAuthority(), this);
auto self = shared_from_this();
SocketIO::getInstance()->addSocket(_uri.getAuthority(), self);
if (_version == SocketIOPacket::SocketIOVersion::V10x)
{
@ -767,14 +778,20 @@ void SIOClientImpl::onOpen(WebSocket* /*ws*/)
_ws->send(s.data());
}
Director::getInstance()->getScheduler()->schedule(CC_SCHEDULE_SELECTOR(SIOClientImpl::heartbeat), this, (_heartbeat * .9f), false);
std::weak_ptr<SIOClientImpl> selfWeak = shared_from_this();
auto f = [selfWeak](float dt) {
auto conn = selfWeak.lock();
if(conn)
conn->heartbeat(dt);
};
Director::getInstance()->getScheduler()->schedule(f, this, (_heartbeat * .9f), false, "heart_beat");
for (auto& client : _clients)
{
client.second->onOpen();
}
CCLOGINFO("SIOClientImpl::onOpen socket connected!");
}
void SIOClientImpl::onMessage(WebSocket* /*ws*/, const WebSocket::Data& data)
@ -1020,11 +1037,9 @@ void SIOClientImpl::onClose(WebSocket* /*ws*/)
_connected = false;
if (Director::getInstance())
Director::getInstance()->getScheduler()->unscheduleAllForTarget(this);
SocketIO::getInstance()->removeSocket(_uri.getAuthority());
}
this->release();
}
void SIOClientImpl::onError(WebSocket* /*ws*/, const WebSocket::ErrorCode& error)
@ -1033,7 +1048,7 @@ void SIOClientImpl::onError(WebSocket* /*ws*/, const WebSocket::ErrorCode& error
}
//begin SIOClient methods
SIOClient::SIOClient(const std::string& path, SIOClientImpl* impl, SocketIO::SIODelegate& delegate)
SIOClient::SIOClient(const std::string& path, std::shared_ptr<SIOClientImpl>& impl, SocketIO::SIODelegate& delegate)
: _path(path)
, _connected(false)
, _socket(impl)
@ -1115,7 +1130,6 @@ void SIOClient::disconnect()
{
setConnected(false);
_socket->disconnectFromEndpoint(_path);
this->release();
}
@ -1123,7 +1137,6 @@ void SIOClient::socketClosed()
{
setConnected(false);
_delegate->onClose(this);
this->release();
}
@ -1132,7 +1145,7 @@ bool SIOClient::isConnected() const
return _socket && _socket->_connected && _connected;
}
void SIOClient::setConnected(bool connected)
void SIOClient::setConnected(bool connected)
{
_connected = connected;
}
@ -1198,8 +1211,8 @@ SIOClient* SocketIO::connect(const std::string& uri, SocketIO::SIODelegate& dele
{
Uri uriObj = Uri::parse(uri);
SIOClientImpl *socket = SocketIO::getInstance()->getSocket(uriObj.getAuthority());
SIOClient *c = nullptr;
std::shared_ptr<SIOClientImpl> socket = SocketIO::getInstance()->getSocket(uriObj.getAuthority());
SIOClient * c = nullptr;
std::string path = uriObj.getPath();
if (path == "")
@ -1235,7 +1248,7 @@ SIOClient* SocketIO::connect(const std::string& uri, SocketIO::SIODelegate& dele
c->disconnect();
CCLOG("SocketIO: recreate a new socket, new client, connect");
SIOClientImpl* newSocket = SIOClientImpl::create(uriObj, caFilePath);
std::shared_ptr<SIOClientImpl> newSocket = SIOClientImpl::create(uriObj, caFilePath);
SIOClient *newC = new (std::nothrow) SIOClient(path, newSocket, delegate);
newSocket->addClient(path, newC);
@ -1248,14 +1261,16 @@ SIOClient* SocketIO::connect(const std::string& uri, SocketIO::SIODelegate& dele
return c;
}
SIOClientImpl* SocketIO::getSocket(const std::string& uri)
std::shared_ptr<SIOClientImpl> SocketIO::getSocket(const std::string& uri)
{
return _sockets.at(uri);
auto p = _sockets.find(uri);
if(p == _sockets.end()) return nullptr;
return p->second.lock();
}
void SocketIO::addSocket(const std::string& uri, SIOClientImpl* socket)
void SocketIO::addSocket(const std::string& uri, std::shared_ptr<SIOClientImpl>& socket)
{
_sockets.insert(uri, socket);
_sockets.emplace(uri, socket);
}
void SocketIO::removeSocket(const std::string& uri)

View File

@ -173,10 +173,11 @@ private:
static SocketIO *_inst;
cocos2d::Map<std::string, SIOClientImpl*> _sockets;
std::unordered_map<std::string, std::weak_ptr<SIOClientImpl
>> _sockets;
SIOClientImpl* getSocket(const std::string& uri);
void addSocket(const std::string& uri, SIOClientImpl* socket);
std::shared_ptr<SIOClientImpl> getSocket(const std::string& uri);
void addSocket(const std::string& uri, std::shared_ptr<SIOClientImpl>& socket);
void removeSocket(const std::string& uri);
friend class SIOClientImpl;
@ -202,9 +203,9 @@ private:
std::string _path, _tag;
bool _connected;
SIOClientImpl* _socket;
SocketIO::SIODelegate* _delegate;
std::shared_ptr<SIOClientImpl> _socket;
SocketIO::SIODelegate* _delegate = nullptr;
EventRegistry _eventRegistry;
@ -228,7 +229,7 @@ private:
* @param impl the SIOClientImpl object.
* @param delegate the SIODelegate object.
*/
SIOClient(const std::string& path, SIOClientImpl* impl, SocketIO::SIODelegate& delegate);
SIOClient(const std::string& path, std::shared_ptr<SIOClientImpl>& impl, SocketIO::SIODelegate& delegate);
/**
* Destructor of SIOClient class.
*/

View File

@ -467,11 +467,11 @@ public:
return false;
}
_data.reserve(LWS_PRE + len);
_data.resize(LWS_PRE, 0x00);
_data.resize(LWS_PRE + len);
if (len > 0)
{
_data.insert(_data.end(), buf, buf + len);
std::copy(buf, buf + len, _data.begin() + LWS_PRE);
}
_payload = _data.data() + LWS_PRE;
@ -544,7 +544,7 @@ WebSocket::WebSocket()
WebSocket::~WebSocket()
{
LOGD("In the destructor of WebSocket (%p)\n", this);
std::lock_guard<std::mutex> lk(__instanceMutex);
if (__websocketInstances != nullptr)