Skip to content

Commit 587bcab

Browse files
PatriceJiangminggo
authored andcommitted
fix asan reported errors (#20332)
1 parent 49bca67 commit 587bcab

File tree

4 files changed

+83
-67
lines changed

4 files changed

+83
-67
lines changed

cocos/2d/CCSprite.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,7 @@ CC_CONSTRUCTOR_ACCESS :
694694
PolygonInfo _polyInfo;
695695

696696
// opacity and RGB protocol
697-
bool _opacityModifyRGB;
697+
bool _opacityModifyRGB = false;
698698

699699
// image is flipped
700700
bool _flippedX = false; /// Whether the sprite is flipped horizontally or not

cocos/network/SocketIO.cpp

Lines changed: 70 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "network/Uri.h"
3333
#include <algorithm>
3434
#include <sstream>
35+
#include <memory>
3536
#include <iterator>
3637
#include "base/ccUTF8.h"
3738
#include "base/CCDirector.h"
@@ -79,8 +80,8 @@ class SocketIOPacket
7980
std::vector<std::string> getData()const{ return _args; };
8081
virtual std::string stringify()const;
8182

82-
static SocketIOPacket * createPacketWithType(const std::string& type, SocketIOVersion version);
83-
static SocketIOPacket * createPacketWithTypeIndex(int type, SocketIOVersion version);
83+
static std::shared_ptr<SocketIOPacket> createPacketWithType(const std::string& type, SocketIOVersion version);
84+
static std::shared_ptr<SocketIOPacket> createPacketWithTypeIndex(int type, SocketIOVersion version);
8485
protected:
8586
std::string _pId;//id message
8687
std::string _ack;//
@@ -305,46 +306,47 @@ SocketIOPacketV10x::~SocketIOPacketV10x()
305306
_endpoint = "";
306307
}
307308

308-
SocketIOPacket * SocketIOPacket::createPacketWithType(const std::string& type, SocketIOPacket::SocketIOVersion version)
309+
std::shared_ptr<SocketIOPacket> SocketIOPacket::createPacketWithType(const std::string& type, SocketIOPacket::SocketIOVersion version)
309310
{
310-
SocketIOPacket *ret;
311-
switch (version)
311+
if(version == SocketIOPacket::SocketIOVersion::V09x)
312312
{
313-
case SocketIOPacket::SocketIOVersion::V09x:
314-
ret = new (std::nothrow) SocketIOPacket;
315-
break;
316-
case SocketIOPacket::SocketIOVersion::V10x:
317-
ret = new (std::nothrow) SocketIOPacketV10x;
318-
break;
313+
auto ret = std::make_shared<SocketIOPacket>();
314+
ret->initWithType(type);
315+
return ret;
319316
}
320-
ret->initWithType(type);
321-
return ret;
317+
else if(version == SocketIOPacket::SocketIOVersion::V10x)
318+
{
319+
auto ret = std::make_shared<SocketIOPacketV10x>();
320+
ret->initWithType(type);
321+
return ret;
322+
}
323+
return nullptr;
322324
}
323325

324-
325-
SocketIOPacket * SocketIOPacket::createPacketWithTypeIndex(int type, SocketIOPacket::SocketIOVersion version)
326+
std::shared_ptr<SocketIOPacket> SocketIOPacket::createPacketWithTypeIndex(int type, SocketIOPacket::SocketIOVersion version)
326327
{
327-
SocketIOPacket *ret;
328-
switch (version)
328+
if(version == SocketIOPacket::SocketIOVersion::V09x)
329329
{
330-
case SocketIOPacket::SocketIOVersion::V09x:
331-
ret = new (std::nothrow) SocketIOPacket;
332-
break;
333-
case SocketIOPacket::SocketIOVersion::V10x:
334-
return new (std::nothrow) SocketIOPacketV10x;
335-
break;
330+
auto ret = std::make_shared<SocketIOPacket>();
331+
ret->initWithTypeIndex(type);
332+
return ret;
333+
}
334+
else if(version == SocketIOPacket::SocketIOVersion::V10x)
335+
{
336+
auto ret = std::make_shared<SocketIOPacketV10x>();
337+
ret->initWithTypeIndex(type);
338+
return ret;
336339
}
337-
ret->initWithTypeIndex(type);
338-
return ret;
340+
return nullptr;
339341
}
340342

341343
/**
342344
* @brief The implementation of the socket.io connection
343345
* Clients/endpoints may share the same impl to accomplish multiplexing on the same websocket
344346
*/
345347
class SIOClientImpl :
346-
public cocos2d::Ref,
347-
public WebSocket::Delegate
348+
public WebSocket::Delegate,
349+
public std::enable_shared_from_this<SIOClientImpl>
348350
{
349351
private:
350352
int _heartbeat, _timeout;
@@ -362,7 +364,7 @@ class SIOClientImpl :
362364
SIOClientImpl(const Uri& uri, const std::string& caFilePath);
363365
virtual ~SIOClientImpl();
364366

365-
static SIOClientImpl* create(const Uri& uri, const std::string& caFilePath);
367+
static std::shared_ptr<SIOClientImpl> create(const Uri& uri, const std::string& caFilePath);
366368

367369
virtual void onOpen(WebSocket* ws);
368370
virtual void onMessage(WebSocket* ws, const WebSocket::Data& data);
@@ -385,7 +387,7 @@ class SIOClientImpl :
385387

386388
void send(const std::string& endpoint, const std::string& s);
387389
void send(const std::string& endpoint, const std::vector<std::string>& s);
388-
void send(SocketIOPacket *packet);
390+
void send(std::shared_ptr<SocketIOPacket>& packet);
389391
void emit(const std::string& endpoint, const std::string& eventname, const std::string& args);
390392
void emit(const std::string& endpoint, const std::string& eventname, const std::vector<std::string>& args);
391393

@@ -429,7 +431,14 @@ void SIOClientImpl::handshake()
429431
request->setUrl(pre.str());
430432
request->setRequestType(HttpRequest::Type::GET);
431433

432-
request->setResponseCallback(CC_CALLBACK_2(SIOClientImpl::handshakeResponse, this));
434+
std::weak_ptr<SIOClientImpl> self = shared_from_this();
435+
auto callback = [self](HttpClient* client, HttpResponse *resp) {
436+
auto conn = self.lock();
437+
if (conn) {
438+
conn->handshakeResponse(client, resp);
439+
}
440+
};
441+
request->setResponseCallback(callback);
433442
request->setTag("handshake");
434443

435444
CCLOGINFO("SIOClientImpl::handshake() waiting");
@@ -633,13 +642,13 @@ void SIOClientImpl::disconnect()
633642
_ws->close();
634643
}
635644

636-
SIOClientImpl* SIOClientImpl::create(const Uri& uri, const std::string& caFilePath)
645+
std::shared_ptr<SIOClientImpl> SIOClientImpl::create(const Uri& uri, const std::string& caFilePath)
637646
{
638647
SIOClientImpl *s = new (std::nothrow) SIOClientImpl(uri, caFilePath);
639648

640649
if (s && s->init())
641650
{
642-
return s;
651+
return std::shared_ptr<SIOClientImpl>(s);
643652
}
644653

645654
return nullptr;
@@ -657,7 +666,7 @@ void SIOClientImpl::addClient(const std::string& endpoint, SIOClient* client)
657666

658667
void SIOClientImpl::connectToEndpoint(const std::string& endpoint)
659668
{
660-
SocketIOPacket *packet = SocketIOPacket::createPacketWithType("connect", _version);
669+
auto packet = SocketIOPacket::createPacketWithType("connect", _version);
661670
packet->setEndpoint(endpoint);
662671
this->send(packet);
663672
}
@@ -685,7 +694,7 @@ void SIOClientImpl::disconnectFromEndpoint(const std::string& endpoint)
685694

686695
void SIOClientImpl::heartbeat(float /*dt*/)
687696
{
688-
SocketIOPacket *packet = SocketIOPacket::createPacketWithType("heartbeat", _version);
697+
auto packet = SocketIOPacket::createPacketWithType("heartbeat", _version);
689698

690699
this->send(packet);
691700

@@ -698,7 +707,7 @@ void SIOClientImpl::send(const std::string& endpoint, const std::vector<std::str
698707
switch (_version) {
699708
case SocketIOPacket::SocketIOVersion::V09x:
700709
{
701-
SocketIOPacket *packet = SocketIOPacket::createPacketWithType("message", _version);
710+
auto packet = SocketIOPacket::createPacketWithType("message", _version);
702711
packet->setEndpoint(endpoint);
703712
for(auto &i : s)
704713
{
@@ -721,7 +730,7 @@ void SIOClientImpl::send(const std::string& endpoint, const std::string& s)
721730
send(endpoint, t);
722731
}
723732

724-
void SIOClientImpl::send(SocketIOPacket *packet)
733+
void SIOClientImpl::send(std::shared_ptr<SocketIOPacket>& packet)
725734
{
726735
std::string req = packet->toString();
727736
if (_connected)
@@ -736,7 +745,7 @@ void SIOClientImpl::send(SocketIOPacket *packet)
736745
void SIOClientImpl::emit(const std::string& endpoint, const std::string& eventname, const std::string& args)
737746
{
738747
CCLOGINFO("Emitting event \"%s\"", eventname.c_str());
739-
SocketIOPacket *packet = SocketIOPacket::createPacketWithType("event", _version);
748+
auto packet = SocketIOPacket::createPacketWithType("event", _version);
740749
packet->setEndpoint(endpoint == "/" ? "" : endpoint);
741750
packet->setEvent(eventname);
742751
packet->addData(args);
@@ -746,7 +755,7 @@ void SIOClientImpl::emit(const std::string& endpoint, const std::string& eventna
746755
void SIOClientImpl::emit(const std::string& endpoint, const std::string& eventname, const std::vector<std::string>& args)
747756
{
748757
CCLOGINFO("Emitting event \"%s\"", eventname.c_str());
749-
SocketIOPacket *packet = SocketIOPacket::createPacketWithType("event", _version);
758+
auto packet = SocketIOPacket::createPacketWithType("event", _version);
750759
packet->setEndpoint(endpoint == "/" ? "" : endpoint);
751760
packet->setEvent(eventname);
752761
for (auto &arg : args) {
@@ -759,22 +768,30 @@ void SIOClientImpl::onOpen(WebSocket* /*ws*/)
759768
{
760769
_connected = true;
761770

762-
SocketIO::getInstance()->addSocket(_uri.getAuthority(), this);
771+
auto self = shared_from_this();
772+
773+
SocketIO::getInstance()->addSocket(_uri.getAuthority(), self);
763774

764775
if (_version == SocketIOPacket::SocketIOVersion::V10x)
765776
{
766777
std::string s = "5";//That's a ping https://github.com/Automattic/engine.io-parser/blob/1b8e077b2218f4947a69f5ad18be2a512ed54e93/lib/index.js#L21
767778
_ws->send(s.data());
768779
}
769780

770-
Director::getInstance()->getScheduler()->schedule(CC_SCHEDULE_SELECTOR(SIOClientImpl::heartbeat), this, (_heartbeat * .9f), false);
781+
std::weak_ptr<SIOClientImpl> selfWeak = shared_from_this();
782+
auto f = [selfWeak](float dt) {
783+
auto conn = selfWeak.lock();
784+
if(conn)
785+
conn->heartbeat(dt);
786+
};
787+
788+
Director::getInstance()->getScheduler()->schedule(f, this, (_heartbeat * .9f), false, "heart_beat");
771789

772790
for (auto& client : _clients)
773791
{
774792
client.second->onOpen();
775793
}
776794

777-
CCLOGINFO("SIOClientImpl::onOpen socket connected!");
778795
}
779796

780797
void SIOClientImpl::onMessage(WebSocket* /*ws*/, const WebSocket::Data& data)
@@ -1020,11 +1037,9 @@ void SIOClientImpl::onClose(WebSocket* /*ws*/)
10201037
_connected = false;
10211038
if (Director::getInstance())
10221039
Director::getInstance()->getScheduler()->unscheduleAllForTarget(this);
1023-
1040+
10241041
SocketIO::getInstance()->removeSocket(_uri.getAuthority());
10251042
}
1026-
1027-
this->release();
10281043
}
10291044

10301045
void SIOClientImpl::onError(WebSocket* /*ws*/, const WebSocket::ErrorCode& error)
@@ -1033,7 +1048,7 @@ void SIOClientImpl::onError(WebSocket* /*ws*/, const WebSocket::ErrorCode& error
10331048
}
10341049

10351050
//begin SIOClient methods
1036-
SIOClient::SIOClient(const std::string& path, SIOClientImpl* impl, SocketIO::SIODelegate& delegate)
1051+
SIOClient::SIOClient(const std::string& path, std::shared_ptr<SIOClientImpl>& impl, SocketIO::SIODelegate& delegate)
10371052
: _path(path)
10381053
, _connected(false)
10391054
, _socket(impl)
@@ -1115,15 +1130,13 @@ void SIOClient::disconnect()
11151130
{
11161131
setConnected(false);
11171132
_socket->disconnectFromEndpoint(_path);
1118-
11191133
this->release();
11201134
}
11211135

11221136
void SIOClient::socketClosed()
11231137
{
11241138
setConnected(false);
11251139
_delegate->onClose(this);
1126-
11271140
this->release();
11281141
}
11291142

@@ -1132,7 +1145,7 @@ bool SIOClient::isConnected() const
11321145
return _socket && _socket->_connected && _connected;
11331146
}
11341147

1135-
void SIOClient::setConnected(bool connected)
1148+
void SIOClient::setConnected(bool connected)
11361149
{
11371150
_connected = connected;
11381151
}
@@ -1198,8 +1211,8 @@ SIOClient* SocketIO::connect(const std::string& uri, SocketIO::SIODelegate& dele
11981211
{
11991212
Uri uriObj = Uri::parse(uri);
12001213

1201-
SIOClientImpl *socket = SocketIO::getInstance()->getSocket(uriObj.getAuthority());
1202-
SIOClient *c = nullptr;
1214+
std::shared_ptr<SIOClientImpl> socket = SocketIO::getInstance()->getSocket(uriObj.getAuthority());
1215+
SIOClient * c = nullptr;
12031216

12041217
std::string path = uriObj.getPath();
12051218
if (path == "")
@@ -1235,7 +1248,7 @@ SIOClient* SocketIO::connect(const std::string& uri, SocketIO::SIODelegate& dele
12351248
c->disconnect();
12361249

12371250
CCLOG("SocketIO: recreate a new socket, new client, connect");
1238-
SIOClientImpl* newSocket = SIOClientImpl::create(uriObj, caFilePath);
1251+
std::shared_ptr<SIOClientImpl> newSocket = SIOClientImpl::create(uriObj, caFilePath);
12391252
SIOClient *newC = new (std::nothrow) SIOClient(path, newSocket, delegate);
12401253

12411254
newSocket->addClient(path, newC);
@@ -1248,14 +1261,16 @@ SIOClient* SocketIO::connect(const std::string& uri, SocketIO::SIODelegate& dele
12481261
return c;
12491262
}
12501263

1251-
SIOClientImpl* SocketIO::getSocket(const std::string& uri)
1264+
std::shared_ptr<SIOClientImpl> SocketIO::getSocket(const std::string& uri)
12521265
{
1253-
return _sockets.at(uri);
1266+
auto p = _sockets.find(uri);
1267+
if(p == _sockets.end()) return nullptr;
1268+
return p->second.lock();
12541269
}
12551270

1256-
void SocketIO::addSocket(const std::string& uri, SIOClientImpl* socket)
1271+
void SocketIO::addSocket(const std::string& uri, std::shared_ptr<SIOClientImpl>& socket)
12571272
{
1258-
_sockets.insert(uri, socket);
1273+
_sockets.emplace(uri, socket);
12591274
}
12601275

12611276
void SocketIO::removeSocket(const std::string& uri)

cocos/network/SocketIO.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,11 @@ class CC_DLL SocketIO
173173

174174
static SocketIO *_inst;
175175

176-
cocos2d::Map<std::string, SIOClientImpl*> _sockets;
176+
std::unordered_map<std::string, std::weak_ptr<SIOClientImpl
177+
>> _sockets;
177178

178-
SIOClientImpl* getSocket(const std::string& uri);
179-
void addSocket(const std::string& uri, SIOClientImpl* socket);
179+
std::shared_ptr<SIOClientImpl> getSocket(const std::string& uri);
180+
void addSocket(const std::string& uri, std::shared_ptr<SIOClientImpl>& socket);
180181
void removeSocket(const std::string& uri);
181182

182183
friend class SIOClientImpl;
@@ -202,9 +203,9 @@ class CC_DLL SIOClient
202203

203204
std::string _path, _tag;
204205
bool _connected;
205-
SIOClientImpl* _socket;
206-
207-
SocketIO::SIODelegate* _delegate;
206+
std::shared_ptr<SIOClientImpl> _socket;
207+
208+
SocketIO::SIODelegate* _delegate = nullptr;
208209

209210
EventRegistry _eventRegistry;
210211

@@ -228,7 +229,7 @@ class CC_DLL SIOClient
228229
* @param impl the SIOClientImpl object.
229230
* @param delegate the SIODelegate object.
230231
*/
231-
SIOClient(const std::string& path, SIOClientImpl* impl, SocketIO::SIODelegate& delegate);
232+
SIOClient(const std::string& path, std::shared_ptr<SIOClientImpl>& impl, SocketIO::SIODelegate& delegate);
232233
/**
233234
* Destructor of SIOClient class.
234235
*/

cocos/network/WebSocket.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -467,11 +467,11 @@ class WebSocketFrame
467467
return false;
468468
}
469469

470-
_data.reserve(LWS_PRE + len);
471-
_data.resize(LWS_PRE, 0x00);
470+
_data.resize(LWS_PRE + len);
471+
472472
if (len > 0)
473473
{
474-
_data.insert(_data.end(), buf, buf + len);
474+
std::copy(buf, buf + len, _data.begin() + LWS_PRE);
475475
}
476476

477477
_payload = _data.data() + LWS_PRE;
@@ -544,7 +544,7 @@ WebSocket::WebSocket()
544544
WebSocket::~WebSocket()
545545
{
546546
LOGD("In the destructor of WebSocket (%p)\n", this);
547-
547+
548548
std::lock_guard<std::mutex> lk(__instanceMutex);
549549

550550
if (__websocketInstances != nullptr)

0 commit comments

Comments
 (0)