Skip to content

Commit d96306a

Browse files
committed
Add suggested exception safety.
1 parent 7a02edc commit d96306a

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

SerialPrograms/Source/Integrations/DiscordSocial/DiscordSocial.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,25 @@ DiscordSocial& DiscordSocial::instance(){
1919
}
2020

2121
void DiscordSocial::run(){
22-
m_client = std::make_shared<Client>();
23-
if (!m_client){
22+
auto client = std::make_shared<Client>();
23+
if (!client){
2424
log("Failed to initialize DiscordSocialSDK.", "run()", LoggingSeverity::Error);
25-
m_client.reset();
2625
return;
2726
}
2827

29-
m_client->SetApplicationId(m_app_id);
30-
m_client->AddLogCallback([&](auto message, auto severity){
31-
log(message, "Internal", severity);
32-
}, m_log_level);
28+
m_client = std::move(client);
29+
try{
30+
m_client->SetApplicationId(m_app_id);
31+
m_client->AddLogCallback([&](auto message, auto severity){
32+
log(message, "Internal", severity);
33+
}, m_log_level);
3334

34-
std::thread(&DiscordSocial::thread_loop, this).detach();
35+
m_thread = std::thread(&DiscordSocial::thread_loop, this);
36+
}catch (...){
37+
m_client.reset();
38+
log("Failed to start DiscordSocialSDK.", "run()", LoggingSeverity::Error);
39+
throw;
40+
}
3541
}
3642

3743
void DiscordSocial::thread_loop(){
@@ -59,9 +65,9 @@ void DiscordSocial::thread_loop(){
5965
}
6066
}
6167

62-
log("Discord Rich Presence update thread exiting...", "thread_loop()", LoggingSeverity::Info);
6368
m_running.store(false, std::memory_order_release);
6469
m_client.reset();
70+
log("Discord Rich Presence update thread exiting...", "thread_loop()", LoggingSeverity::Info);
6571
}
6672

6773
void DiscordSocial::update_rich_presence(){
@@ -111,8 +117,8 @@ void DiscordSocial::update_rich_presence(){
111117
}
112118
});
113119
}catch (const std::exception& e){
114-
log("Exception: " + std::string(e.what()), "update_rp()", LoggingSeverity::Error);
115120
m_running.store(false, std::memory_order_release);
121+
log("Exception: " + std::string(e.what()), "update_rp()", LoggingSeverity::Error);
116122
}
117123
}
118124

SerialPrograms/Source/Integrations/DiscordSocial/DiscordSocial.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#ifndef DISCORD_SOCIAL_H
33
#define DISCORD_SOCIAL_H
44

5+
#include <thread>
56
#include <atomic>
67
#include <discord_social_sdk/discordpp.h>
78
#include "Common/Cpp/AbstractLogger.h"
@@ -14,6 +15,7 @@ namespace DiscordSocialSDK{
1415
DiscordSocial() : m_running(false) {}
1516
~DiscordSocial(){
1617
m_running.store(false, std::memory_order_release);
18+
m_thread.join();
1719
if (m_client) m_client.reset();
1820
}
1921

@@ -28,6 +30,7 @@ namespace DiscordSocialSDK{
2830
void thread_loop();
2931

3032
private:
33+
std::thread m_thread;
3134
std::shared_ptr<discordpp::Client> m_client = nullptr;
3235
std::atomic<bool> m_running;
3336
const uint64_t m_app_id = 1406867596585865326;

0 commit comments

Comments
 (0)