Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* ! Minor breaking change ! Default automatic session update duration changed to 60 seconds.
* Added a call to change the automatic session update duration.
* Fixed a bug where session duration was reported wrong.
* Fixed a bug that caused an exception when the application quit. This was due to the SDK attempting to send an end session request.
* Fixed an issue with the custom HTTP client function pointer by setting it's default value.
* Fixed a bug that caused GET requests to fail on Linux.
* Fixed bug when changing device id with server merge.
Expand Down
2 changes: 2 additions & 0 deletions include/countly.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ class Countly {
_auto_session_update_interval = updateInterval;
}
private:
void _deleteThread();
void log(LogLevel level, const std::string& message);

HTTPResponse sendHTTP(std::string path, std::string data);
Expand All @@ -234,6 +235,7 @@ class Countly {
int port;
bool use_https;
bool always_use_post;
bool is_being_disposed;
std::chrono::system_clock::time_point last_sent_session_request;
bool began_session;

Expand Down
22 changes: 18 additions & 4 deletions src/countly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Countly::Countly() : max_events(COUNTLY_MAX_EVENTS_DEFAULT), wait_milliseconds(C
stop_thread = false;
began_session = false;
always_use_post = false;
is_being_disposed = false;
remote_config_enabled = false;

//Petting to null values
Expand All @@ -49,6 +50,7 @@ Countly::Countly() : max_events(COUNTLY_MAX_EVENTS_DEFAULT), wait_milliseconds(C
}

Countly::~Countly() {
is_being_disposed = true;
stop();
#if !defined(_WIN32) && !defined(COUNTLY_USE_CUSTOM_HTTP)
curl_global_cleanup();
Expand Down Expand Up @@ -289,21 +291,26 @@ void Countly::startOnCloud(const std::string& app_key) {
}

void Countly::stop() {
_deleteThread();
if (began_session) {
endSession();
}
}

void Countly::_deleteThread() {
mutex.lock();
stop_thread = true;
mutex.unlock();
if (thread != nullptr && thread->joinable()) {
try {
thread->join();
} catch(const std::system_error& e) {
}
catch (const std::system_error& e) {
log(Countly::LogLevel::WARNING, "Could not join thread");
}
delete thread;
thread = nullptr;
}
if (began_session) {
endSession();
}
}

void Countly::setUpdateInterval(size_t milliseconds) {
Expand Down Expand Up @@ -607,6 +614,13 @@ bool Countly::endSession() {
{"timestamp", std::to_string(timestamp.count())},
{"end_session", "1"}
};

if (is_being_disposed) {
// if SDK is being destroyed, don't attempt to send the end-session request.
mutex.unlock();
return false;
}

if (sendHTTP("/i", Countly::serializeForm(data)).success) {
last_sent_session_request = now;
began_session = false;
Expand Down