Dynamically create IoThread... (#179)

So that it doesn't get deleted before Discord is destroyed.
This commit is contained in:
Ted John 2018-05-16 21:21:16 +01:00 committed by Mason Sciotti
parent cac0362377
commit ba9fe00c4d

View file

@ -118,7 +118,7 @@ public:
void Notify() {} void Notify() {}
}; };
#endif // DISCORD_DISABLE_IO_THREAD #endif // DISCORD_DISABLE_IO_THREAD
static IoThreadHolder IoThread; static IoThreadHolder* IoThread{nullptr};
static void UpdateReconnectTime() static void UpdateReconnectTime()
{ {
@ -237,7 +237,9 @@ static void Discord_UpdateConnection(void)
static void SignalIOActivity() static void SignalIOActivity()
{ {
IoThread.Notify(); if (IoThread != nullptr) {
IoThread->Notify();
}
} }
static bool RegisterForEvent(const char* evtName) static bool RegisterForEvent(const char* evtName)
@ -271,6 +273,11 @@ extern "C" DISCORD_EXPORT void Discord_Initialize(const char* applicationId,
int autoRegister, int autoRegister,
const char* optionalSteamId) const char* optionalSteamId)
{ {
IoThread = new (std::nothrow) IoThreadHolder();
if (IoThread == nullptr) {
return;
}
if (autoRegister) { if (autoRegister) {
if (optionalSteamId && optionalSteamId[0]) { if (optionalSteamId && optionalSteamId[0]) {
Discord_RegisterSteamGame(applicationId, optionalSteamId); Discord_RegisterSteamGame(applicationId, optionalSteamId);
@ -335,7 +342,7 @@ extern "C" DISCORD_EXPORT void Discord_Initialize(const char* applicationId,
UpdateReconnectTime(); UpdateReconnectTime();
}; };
IoThread.Start(); IoThread->Start();
} }
extern "C" DISCORD_EXPORT void Discord_Shutdown(void) extern "C" DISCORD_EXPORT void Discord_Shutdown(void)
@ -346,7 +353,12 @@ extern "C" DISCORD_EXPORT void Discord_Shutdown(void)
Connection->onConnect = nullptr; Connection->onConnect = nullptr;
Connection->onDisconnect = nullptr; Connection->onDisconnect = nullptr;
Handlers = {}; Handlers = {};
IoThread.Stop(); if (IoThread != nullptr) {
IoThread->Stop();
delete IoThread;
IoThread = nullptr;
}
RpcConnection::Destroy(Connection); RpcConnection::Destroy(Connection);
} }