@@ -157,18 +157,18 @@ class APClient {
157
157
}
158
158
}
159
159
160
- #ifndef AP_NO_DEFAULT_DATA_PACKAGE_STORE
161
160
if (!_dataPackageStore) {
162
- _dataPackageStore = new DefaultDataPackageStore ();
163
- _dataPackageStoreAllocated = true ;
164
- }
161
+ # ifndef AP_NO_DEFAULT_DATA_PACKAGE_STORE
162
+ _autoDataPackageStore. reset ( new DefaultDataPackageStore ()) ;
163
+ _dataPackageStore =_autoDataPackageStore. get ();
165
164
#else
166
- const char * msg = " dataPackageStore is required if compiled with AP_NO_DEFAULT_DATA_PACKAGE_STORE" ;
167
- fprintf (stderr, " APClient: %s!\n " , msg);
168
- #ifdef __cpp_exceptions
169
- throw std::runtime_error (msg);
170
- #endif
165
+ const char * msg = " dataPackageStore is required if compiled with AP_NO_DEFAULT_DATA_PACKAGE_STORE" ;
166
+ fprintf (stderr, " APClient: %s!\n " , msg);
167
+ #ifdef __cpp_exceptions
168
+ throw std::runtime_error (msg);
169
+ #endif
171
170
#endif
171
+ }
172
172
173
173
_uuid = uuid;
174
174
_game = game;
@@ -185,12 +185,6 @@ class APClient {
185
185
186
186
virtual ~APClient ()
187
187
{
188
- #ifndef AP_NO_DEFAULT_DATA_PACKAGE_STORE
189
- if (_dataPackageStoreAllocated)
190
- delete _dataPackageStore;
191
- #endif
192
- delete _ws;
193
- _ws = nullptr ;
194
188
}
195
189
196
190
enum class State {
@@ -1103,11 +1097,10 @@ class APClient {
1103
1097
*/
1104
1098
void poll ()
1105
1099
{
1106
- if (_ws && _state == State::DISCONNECTED) {
1107
- delete _ws;
1108
- _ws = nullptr ;
1109
- }
1110
- if (_ws) _ws->poll ();
1100
+ if (_ws && _state == State::DISCONNECTED)
1101
+ _ws.reset ();
1102
+ if (_ws)
1103
+ _ws->poll ();
1111
1104
if (_state < State::SOCKET_CONNECTED) {
1112
1105
auto t = now ();
1113
1106
if (t - _lastSocketConnect > _socketReconnectInterval || _reconnectNow) {
@@ -1134,8 +1127,7 @@ class APClient {
1134
1127
_hintCostPercent = 0 ;
1135
1128
_hintPoints = 0 ;
1136
1129
_players.clear ();
1137
- delete _ws;
1138
- _ws = nullptr ;
1130
+ _ws.reset ();
1139
1131
_state = State::DISCONNECTED;
1140
1132
_hasPassword = false ;
1141
1133
}
@@ -1499,7 +1491,7 @@ class APClient {
1499
1491
void connect_socket ()
1500
1492
{
1501
1493
_reconnectNow = false ;
1502
- delete _ws;
1494
+ _ws. reset () ;
1503
1495
if (_uri.empty ()) {
1504
1496
_ws = nullptr ;
1505
1497
_state = State::DISCONNECTED;
@@ -1508,7 +1500,7 @@ class APClient {
1508
1500
_state = State::SOCKET_CONNECTING;
1509
1501
1510
1502
try {
1511
- _ws = new WS (_uri,
1503
+ _ws. reset ( new WS (_uri,
1512
1504
[this ]() { onopen (); },
1513
1505
[this ]() { onclose (); },
1514
1506
[this ](const std::string& s) { onmessage (s); },
@@ -1520,7 +1512,7 @@ class APClient {
1520
1512
#if WSWRAP_VERSION >= 10100
1521
1513
, _certStore
1522
1514
#endif
1523
- );
1515
+ )) ;
1524
1516
} catch (const std::exception& ex) {
1525
1517
_ws = nullptr ;
1526
1518
if (_tryWSS && _uri.rfind (" ws://" , 0 ) == 0 ) {
@@ -1599,7 +1591,7 @@ class APClient {
1599
1591
std::string _game;
1600
1592
std::string _uuid;
1601
1593
std::string _certStore;
1602
- WS* _ws = nullptr ;
1594
+ std::unique_ptr<WS> _ws;
1603
1595
State _state = State::DISCONNECTED;
1604
1596
bool _tryWSS = false ;
1605
1597
@@ -1648,7 +1640,7 @@ class APClient {
1648
1640
std::set<int64_t > _missingLocations;
1649
1641
APDataPackageStore* _dataPackageStore;
1650
1642
#ifndef AP_NO_DEFAULT_DATA_PACKAGE_STORE
1651
- bool _dataPackageStoreAllocated = false ;
1643
+ std::unique_ptr<APDataPackageStore> _autoDataPackageStore ;
1652
1644
#endif
1653
1645
std::map<int , NetworkSlot> _slotInfo;
1654
1646
0 commit comments