Skip to content

Commit bef2feb

Browse files
ZahidZafarborgbyte
andauthored
Tweaked "Refactor member thread as unique_ptr" PR (#60)
* Refactor to in-class member initializer * Refactor member thread as unique_ptr * fixing potential bug. Co-authored-by: Borgbyte <[email protected]>
1 parent 5d259b9 commit bef2feb

File tree

2 files changed

+22
-38
lines changed

2 files changed

+22
-38
lines changed

include/countly.hpp

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33

44
#include "countly/constants.hpp"
55

6-
#include <iterator>
76
#include <chrono>
7+
#include <iterator>
8+
#include <map>
9+
#include <memory>
10+
#include <mutex>
811
#include <string>
912
#include <thread>
10-
#include <mutex>
11-
#include <map>
1213

1314
#ifndef COUNTLY_USE_SQLITE
1415
#include <deque>
@@ -244,39 +245,39 @@ class Countly {
244245

245246
void updateLoop();
246247

247-
void (*logger_function)(LogLevel level, const std::string& message);
248-
HTTPResponse (*http_client_function)(bool is_post, const std::string& url, const std::string& data);
248+
void (*logger_function)(LogLevel level, const std::string& message) = nullptr;
249+
HTTPResponse (*http_client_function)(bool is_post, const std::string& url, const std::string& data) = nullptr;
249250

250251
std::string host;
251252

252-
int port;
253-
bool use_https;
254-
bool always_use_post;
253+
int port = 0;
254+
bool use_https = false;
255+
bool always_use_post = false;
255256

256-
bool began_session;
257-
bool is_being_disposed;
258-
bool is_sdk_initialized;
257+
bool began_session = false;
258+
bool is_being_disposed = false;
259+
bool is_sdk_initialized = false;
259260

260261
std::chrono::system_clock::time_point last_sent_session_request;
261262

262263
json session_params;
263264
std::string salt;
264265

265-
std::thread *thread;
266+
std::unique_ptr<std::thread> thread;
266267
std::mutex mutex;
267-
bool stop_thread;
268-
bool running;
269-
size_t wait_milliseconds;
268+
bool stop_thread = false;
269+
bool running = false;
270+
size_t wait_milliseconds = COUNTLY_KEEPALIVE_INTERVAL;
270271
unsigned short _auto_session_update_interval = 60; // value is in seconds;
271272

272-
size_t max_events;
273+
size_t max_events = COUNTLY_MAX_EVENTS_DEFAULT;
273274
#ifndef COUNTLY_USE_SQLITE
274275
std::deque<std::string> event_queue;
275276
#else
276277
std::string database_path;
277278
#endif
278279

279-
bool remote_config_enabled;
280+
bool remote_config_enabled = false;
280281
json remote_config;
281282
};
282283

src/countly.cpp

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,7 @@ using json = nlohmann::json;
2828
#include "sqlite3.h"
2929
#endif
3030

31-
Countly::Countly() : max_events(COUNTLY_MAX_EVENTS_DEFAULT), wait_milliseconds(COUNTLY_KEEPALIVE_INTERVAL) {
32-
//Setting the default values
33-
port = 0;
34-
running = false;
35-
use_https = false;
36-
stop_thread = false;
37-
began_session = false;
38-
always_use_post = false;
39-
is_being_disposed = false;
40-
is_sdk_initialized = false;
41-
remote_config_enabled = false;
42-
43-
//Petting to null values
44-
thread = nullptr;
45-
logger_function = nullptr;
46-
http_client_function = nullptr;
47-
31+
Countly::Countly() {
4832
#if !defined(_WIN32) && !defined(COUNTLY_USE_CUSTOM_HTTP)
4933
curl_global_init(CURL_GLOBAL_ALL);
5034
#endif
@@ -354,7 +338,7 @@ void Countly::start(const std::string& app_key, const std::string& host, int por
354338
stop_thread = false;
355339

356340
try {
357-
thread = new std::thread(&Countly::updateLoop, this);
341+
thread.reset(new std::thread(&Countly::updateLoop, this));
358342
} catch(const std::system_error& e) {
359343
std::ostringstream log_message;
360344
log_message << "Could not create thread: " << e.what();
@@ -385,15 +369,14 @@ void Countly::_deleteThread() {
385369
mutex.lock();
386370
stop_thread = true;
387371
mutex.unlock();
388-
if (thread != nullptr && thread->joinable()) {
372+
if (thread && thread->joinable()) {
389373
try {
390374
thread->join();
391375
}
392376
catch (const std::system_error& e) {
393377
log(Countly::LogLevel::WARNING, "Could not join thread");
394378
}
395-
delete thread;
396-
thread = nullptr;
379+
thread.reset();
397380
}
398381
}
399382

0 commit comments

Comments
 (0)