Skip to content

Commit b41d08b

Browse files
[Sdk 661] Stop calling endsession on instance dispose (#44)
* Do not send endsession request on instance dispose * function moved to top * change log entry * function name updated * Change log updated * PR Changes * Update CHANGELOG.md Co-authored-by: ArtursKadikis <[email protected]>
1 parent 344ca96 commit b41d08b

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* ! Minor breaking change ! Default automatic session update duration changed to 60 seconds.
44
* Added a call to change the automatic session update duration.
55
* Fixed a bug where session duration was reported wrong.
6+
* Fixed a bug that caused an exception when the application quit. This was due to the SDK attempting to send an end session request.
67
* Fixed an issue with the custom HTTP client function pointer by setting it's default value.
78
* Fixed a bug that caused GET requests to fail on Linux.
89
* Fixed bug when changing device id with server merge.

include/countly.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ class Countly {
213213
_auto_session_update_interval = updateInterval;
214214
}
215215
private:
216+
void _deleteThread();
216217
void log(LogLevel level, const std::string& message);
217218

218219
HTTPResponse sendHTTP(std::string path, std::string data);
@@ -234,6 +235,7 @@ class Countly {
234235
int port;
235236
bool use_https;
236237
bool always_use_post;
238+
bool is_being_disposed;
237239
std::chrono::system_clock::time_point last_sent_session_request;
238240
bool began_session;
239241

src/countly.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Countly::Countly() : max_events(COUNTLY_MAX_EVENTS_DEFAULT), wait_milliseconds(C
3636
stop_thread = false;
3737
began_session = false;
3838
always_use_post = false;
39+
is_being_disposed = false;
3940
remote_config_enabled = false;
4041

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

5152
Countly::~Countly() {
53+
is_being_disposed = true;
5254
stop();
5355
#if !defined(_WIN32) && !defined(COUNTLY_USE_CUSTOM_HTTP)
5456
curl_global_cleanup();
@@ -289,21 +291,26 @@ void Countly::startOnCloud(const std::string& app_key) {
289291
}
290292

291293
void Countly::stop() {
294+
_deleteThread();
295+
if (began_session) {
296+
endSession();
297+
}
298+
}
299+
300+
void Countly::_deleteThread() {
292301
mutex.lock();
293302
stop_thread = true;
294303
mutex.unlock();
295304
if (thread != nullptr && thread->joinable()) {
296305
try {
297306
thread->join();
298-
} catch(const std::system_error& e) {
307+
}
308+
catch (const std::system_error& e) {
299309
log(Countly::LogLevel::WARNING, "Could not join thread");
300310
}
301311
delete thread;
302312
thread = nullptr;
303313
}
304-
if (began_session) {
305-
endSession();
306-
}
307314
}
308315

309316
void Countly::setUpdateInterval(size_t milliseconds) {
@@ -607,6 +614,13 @@ bool Countly::endSession() {
607614
{"timestamp", std::to_string(timestamp.count())},
608615
{"end_session", "1"}
609616
};
617+
618+
if (is_being_disposed) {
619+
// if SDK is being destroyed, don't attempt to send the end-session request.
620+
mutex.unlock();
621+
return false;
622+
}
623+
610624
if (sendHTTP("/i", Countly::serializeForm(data)).success) {
611625
last_sent_session_request = now;
612626
began_session = false;

0 commit comments

Comments
 (0)