From 044d457d20c6eb086cbc2a433b6f3187fff66e95 Mon Sep 17 00:00:00 2001 From: nerix Date: Sat, 23 Mar 2024 11:56:42 +0100 Subject: [PATCH] fix: create NetworkManager statics in `init()` (#5254) --- CHANGELOG.md | 1 + src/common/network/NetworkManager.cpp | 29 +++++++++++++++++++++------ src/common/network/NetworkManager.hpp | 4 ++-- src/common/network/NetworkPrivate.cpp | 2 +- src/common/network/NetworkTask.cpp | 18 ++++++++--------- tests/src/NetworkRequest.cpp | 24 +++++++++++----------- 6 files changed, 48 insertions(+), 30 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 56fd13093..9496106cc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -188,6 +188,7 @@ - Dev: Cleaned up and optimized resources. (#5222) - Dev: Refactor `StreamerMode`. (#5216, #5236) - Dev: Cleaned up unused code in `MessageElement` and `MessageLayoutElement`. (#5225) +- Dev: `NetworkManager`'s statics are now created in its `init` method. (#5254) ## 2.4.6 diff --git a/src/common/network/NetworkManager.cpp b/src/common/network/NetworkManager.cpp index dfc9fe0a0..956c2e79f 100644 --- a/src/common/network/NetworkManager.cpp +++ b/src/common/network/NetworkManager.cpp @@ -4,19 +4,36 @@ namespace chatterino { -QThread NetworkManager::workerThread; -QNetworkAccessManager NetworkManager::accessManager; +QThread *NetworkManager::workerThread = nullptr; +QNetworkAccessManager *NetworkManager::accessManager = nullptr; void NetworkManager::init() { - NetworkManager::accessManager.moveToThread(&NetworkManager::workerThread); - NetworkManager::workerThread.start(); + assert(!NetworkManager::workerThread); + assert(!NetworkManager::accessManager); + + NetworkManager::workerThread = new QThread; + NetworkManager::workerThread->start(); + + NetworkManager::accessManager = new QNetworkAccessManager; + NetworkManager::accessManager->moveToThread(NetworkManager::workerThread); } void NetworkManager::deinit() { - NetworkManager::workerThread.quit(); - NetworkManager::workerThread.wait(); + assert(NetworkManager::workerThread); + assert(NetworkManager::accessManager); + + if (NetworkManager::workerThread) + { + NetworkManager::workerThread->quit(); + NetworkManager::workerThread->wait(); + } + + delete NetworkManager::accessManager; + NetworkManager::accessManager = nullptr; + delete NetworkManager::workerThread; + NetworkManager::workerThread = nullptr; } } // namespace chatterino diff --git a/src/common/network/NetworkManager.hpp b/src/common/network/NetworkManager.hpp index 530aaae1f..b02ce04e5 100644 --- a/src/common/network/NetworkManager.hpp +++ b/src/common/network/NetworkManager.hpp @@ -10,8 +10,8 @@ class NetworkManager : public QObject Q_OBJECT public: - static QThread workerThread; - static QNetworkAccessManager accessManager; + static QThread *workerThread; + static QNetworkAccessManager *accessManager; static void init(); static void deinit(); diff --git a/src/common/network/NetworkPrivate.cpp b/src/common/network/NetworkPrivate.cpp index adf46b6f7..51842dd57 100644 --- a/src/common/network/NetworkPrivate.cpp +++ b/src/common/network/NetworkPrivate.cpp @@ -48,7 +48,7 @@ void loadUncached(std::shared_ptr &&data) NetworkRequester requester; auto *worker = new NetworkTask(std::move(data)); - worker->moveToThread(&NetworkManager::workerThread); + worker->moveToThread(NetworkManager::workerThread); QObject::connect(&requester, &NetworkRequester::requestUrl, worker, &NetworkTask::run); diff --git a/src/common/network/NetworkTask.cpp b/src/common/network/NetworkTask.cpp index 7590c8a46..256743a4f 100644 --- a/src/common/network/NetworkTask.cpp +++ b/src/common/network/NetworkTask.cpp @@ -54,41 +54,41 @@ QNetworkReply *NetworkTask::createReply() { const auto &data = this->data_; const auto &request = this->data_->request; - auto &accessManager = NetworkManager::accessManager; + auto *accessManager = NetworkManager::accessManager; switch (this->data_->requestType) { case NetworkRequestType::Get: - return accessManager.get(request); + return accessManager->get(request); case NetworkRequestType::Put: - return accessManager.put(request, data->payload); + return accessManager->put(request, data->payload); case NetworkRequestType::Delete: - return accessManager.deleteResource(data->request); + return accessManager->deleteResource(data->request); case NetworkRequestType::Post: if (data->multiPartPayload) { assert(data->payload.isNull()); - return accessManager.post(request, - data->multiPartPayload.get()); + return accessManager->post(request, + data->multiPartPayload.get()); } else { - return accessManager.post(request, data->payload); + return accessManager->post(request, data->payload); } case NetworkRequestType::Patch: if (data->multiPartPayload) { assert(data->payload.isNull()); - return accessManager.sendCustomRequest( + return accessManager->sendCustomRequest( request, "PATCH", data->multiPartPayload.get()); } else { - return NetworkManager::accessManager.sendCustomRequest( + return NetworkManager::accessManager->sendCustomRequest( request, "PATCH", data->payload); } } diff --git a/tests/src/NetworkRequest.cpp b/tests/src/NetworkRequest.cpp index 7029488af..2f6b8102f 100644 --- a/tests/src/NetworkRequest.cpp +++ b/tests/src/NetworkRequest.cpp @@ -74,7 +74,7 @@ TEST(NetworkRequest, Success) { const std::vector codes{200, 201, 202, 203, 204, 205, 206}; - EXPECT_TRUE(NetworkManager::workerThread.isRunning()); + EXPECT_TRUE(NetworkManager::workerThread->isRunning()); for (const auto code : codes) { @@ -96,14 +96,14 @@ TEST(NetworkRequest, Success) waiter.waitForRequest(); } - EXPECT_TRUE(NetworkManager::workerThread.isRunning()); + EXPECT_TRUE(NetworkManager::workerThread->isRunning()); } TEST(NetworkRequest, FinallyCallbackOnSuccess) { const std::vector codes{200, 201, 202, 203, 204, 205, 206}; - EXPECT_TRUE(NetworkManager::workerThread.isRunning()); + EXPECT_TRUE(NetworkManager::workerThread->isRunning()); for (const auto code : codes) { @@ -132,7 +132,7 @@ TEST(NetworkRequest, Error) 411, 412, 413, 414, 418, 500, 501, 502, 503, 504, }; - EXPECT_TRUE(NetworkManager::workerThread.isRunning()); + EXPECT_TRUE(NetworkManager::workerThread->isRunning()); for (const auto code : codes) { @@ -155,7 +155,7 @@ TEST(NetworkRequest, Error) waiter.waitForRequest(); } - EXPECT_TRUE(NetworkManager::workerThread.isRunning()); + EXPECT_TRUE(NetworkManager::workerThread->isRunning()); } TEST(NetworkRequest, FinallyCallbackOnError) @@ -165,7 +165,7 @@ TEST(NetworkRequest, FinallyCallbackOnError) 411, 412, 413, 414, 418, 500, 501, 502, 503, 504, }; - EXPECT_TRUE(NetworkManager::workerThread.isRunning()); + EXPECT_TRUE(NetworkManager::workerThread->isRunning()); for (const auto code : codes) { @@ -189,7 +189,7 @@ TEST(NetworkRequest, FinallyCallbackOnError) TEST(NetworkRequest, TimeoutTimingOut) { - EXPECT_TRUE(NetworkManager::workerThread.isRunning()); + EXPECT_TRUE(NetworkManager::workerThread->isRunning()); auto url = getDelayURL(5); RequestWaiter waiter; @@ -214,12 +214,12 @@ TEST(NetworkRequest, TimeoutTimingOut) waiter.waitForRequest(); - EXPECT_TRUE(NetworkManager::workerThread.isRunning()); + EXPECT_TRUE(NetworkManager::workerThread->isRunning()); } TEST(NetworkRequest, TimeoutNotTimingOut) { - EXPECT_TRUE(NetworkManager::workerThread.isRunning()); + EXPECT_TRUE(NetworkManager::workerThread->isRunning()); auto url = getDelayURL(1); RequestWaiter waiter; @@ -240,12 +240,12 @@ TEST(NetworkRequest, TimeoutNotTimingOut) waiter.waitForRequest(); - EXPECT_TRUE(NetworkManager::workerThread.isRunning()); + EXPECT_TRUE(NetworkManager::workerThread->isRunning()); } TEST(NetworkRequest, FinallyCallbackOnTimeout) { - EXPECT_TRUE(NetworkManager::workerThread.isRunning()); + EXPECT_TRUE(NetworkManager::workerThread->isRunning()); auto url = getDelayURL(5); @@ -276,5 +276,5 @@ TEST(NetworkRequest, FinallyCallbackOnTimeout) EXPECT_TRUE(finallyCalled); EXPECT_TRUE(onErrorCalled); EXPECT_FALSE(onSuccessCalled); - EXPECT_TRUE(NetworkManager::workerThread.isRunning()); + EXPECT_TRUE(NetworkManager::workerThread->isRunning()); }