From 2c1d02095ab714998a044685be9e42e96013655c Mon Sep 17 00:00:00 2001 From: unitraxx Date: Thu, 12 Sep 2013 18:23:56 +0000 Subject: [PATCH] Changes to HTTPManager and it's clean-up sequence. Main thread now "joins" the network thread and thus waits for all the requests to be done. (Remember me to start a discussion on the memory leak checker!) git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/branches/uni@13671 178a84e3-b1eb-0310-8ba1-8eac791a3b58 --- src/main.cpp | 2 ++ src/online/current_user.cpp | 4 ++-- src/online/http_manager.cpp | 19 ++++++++++++------- src/online/http_manager.hpp | 4 ++-- src/online/request.cpp | 2 +- src/online/request.hpp | 4 +--- 6 files changed, 20 insertions(+), 15 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 5e3b82276..68761a5da 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1191,6 +1191,7 @@ void initRest() // to network_http (since the thread might use network_http, otherwise // a race condition can be introduced resulting in a crash). INetworkHttp::get()->startNetworkThread(); + Online::HTTPManager::get()->startNetworkThread(); music_manager = new MusicManager(); sfx_manager = new SFXManager(); // The order here can be important, e.g. KartPropertiesManager needs @@ -1297,6 +1298,7 @@ void cleanSuperTuxKart() StateManager::deallocate(); GUIEngine::EventHandler::deallocate(); + Online::HTTPManager::deallocate(); } // cleanSuperTuxKart //============================================================================= diff --git a/src/online/current_user.cpp b/src/online/current_user.cpp index 14dc2bf67..a2f54b602 100644 --- a/src/online/current_user.cpp +++ b/src/online/current_user.cpp @@ -229,7 +229,7 @@ namespace Online{ { if(!success) { - Log::warn("CurrentUser::signOut", "%s", _("There were some connection issues while signing out. Report a bug if this caused issues.")); + Log::warn("CurrentUser::signOut", "%s", "There were some connection issues while signing out. Report a bug if this caused issues."); } m_token = ""; ProfileManager::get()->clearPersistent(); @@ -623,7 +623,7 @@ namespace Online{ { if(isRegisteredUser()) { - XMLRequest * request = new XMLRequest(true, HTTPManager::MAX_PRIORITY); + HTTPRequest * request = new HTTPRequest(true, HTTPManager::MAX_PRIORITY); request->setURL((std::string)UserConfigParams::m_server_multiplayer + "client-user.php"); request->setParameter("action", std::string("client-quit")); request->setParameter("token", getToken()); diff --git a/src/online/http_manager.cpp b/src/online/http_manager.cpp index 28c686ed2..c1671e9eb 100644 --- a/src/online/http_manager.cpp +++ b/src/online/http_manager.cpp @@ -57,16 +57,17 @@ namespace Online{ if (http_singleton == NULL) { http_singleton = new HTTPManager(); - http_singleton->startNetworkThread(); - CurrentUser::get()->requestSavedSession(); } return http_singleton; } // get void HTTPManager::deallocate() { - delete http_singleton; - http_singleton = NULL; + if (http_singleton != NULL) + { + delete http_singleton; + http_singleton = NULL; + } } // deallocate bool HTTPManager::isRunning() @@ -84,6 +85,11 @@ namespace Online{ // ============================================================================ HTTPManager::~HTTPManager(){ + m_thread_id.lock(); + pthread_join(*m_thread_id.getData(), NULL); + delete m_thread_id.getData(); + m_thread_id.unlock(); + pthread_cond_destroy(&m_cond_request); curl_global_cleanup(); } @@ -116,10 +122,10 @@ namespace Online{ Log::error("HTTP Manager", "Could not create thread, error=%d.\n", errno); } pthread_attr_destroy(&attr); + CurrentUser::get()->requestSavedSession(); } // startNetworkThread - // --------------------------------------------------------------------------- /** This function inserts a high priority request to quit into the request * queue of the network thead, and also aborts any ongoing download. @@ -188,7 +194,7 @@ namespace Online{ { HTTPManager *me = (HTTPManager*) obj; - pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); + //pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); me->m_current_request = NULL; me->m_request_queue.lock(); @@ -221,7 +227,6 @@ namespace Online{ delete request; } me->m_request_queue.unlock(); - HTTPManager::deallocate(); pthread_exit(NULL); return 0; } // mainLoop diff --git a/src/online/http_manager.hpp b/src/online/http_manager.hpp index 3cd52b869..31f00e683 100644 --- a/src/online/http_manager.hpp +++ b/src/online/http_manager.hpp @@ -76,8 +76,6 @@ namespace Online{ void handleResultQueue(); static void *mainLoop(void *obj); - void startNetworkThread(); - static void deallocate(); HTTPManager(); //const std::string &url ~HTTPManager(); @@ -87,6 +85,7 @@ namespace Online{ // singleton static HTTPManager* get(); + static void deallocate(); static bool isRunning(); //Execute @@ -96,6 +95,7 @@ namespace Online{ void synchronousRequest(Online::Request *request); void addRequest(Online::Request *request); void cancelAllDownloads(); + void startNetworkThread(); void stopNetworkThread(); bool getAbort(){ return m_abort.getAtomic(); }; diff --git a/src/online/request.cpp b/src/online/request.cpp index bb7f5debb..b42bc96c8 100644 --- a/src/online/request.cpp +++ b/src/online/request.cpp @@ -103,6 +103,7 @@ namespace Online{ curl_easy_setopt(m_curl_session, CURLOPT_CAINFO, (file_manager->getDataDir() + "web.tuxfamily.org.pem").c_str()); curl_easy_setopt(m_curl_session, CURLOPT_SSL_VERIFYPEER, 0L); //curl_easy_setopt(m_curl_session, CURLOPT_VERBOSE, 1L); + curl_easy_setopt(m_curl_session, CURLOPT_WRITEDATA, &m_string_buffer); } void HTTPRequest::operation() @@ -222,7 +223,6 @@ namespace Online{ void XMLRequest::prepareOperation() { HTTPRequest::prepareOperation(); - curl_easy_setopt(m_curl_session, CURLOPT_WRITEDATA, &m_string_buffer); } diff --git a/src/online/request.hpp b/src/online/request.hpp index 9576e238c..5ff8372e3 100644 --- a/src/online/request.hpp +++ b/src/online/request.hpp @@ -150,6 +150,7 @@ namespace Online{ Parameters * m_parameters; CURL * m_curl_session; CURLcode m_curl_code; + std::string m_string_buffer; virtual void prepareOperation() OVERRIDE; virtual void operation() OVERRIDE; @@ -201,9 +202,6 @@ namespace Online{ class XMLRequest : public HTTPRequest { - private: - std::string m_string_buffer; - protected : XMLNode * m_result; irr::core::stringw m_info;