From 7500ab6c4c301d82ce5e6e6c6fdb8a8e3aac2876 Mon Sep 17 00:00:00 2001 From: Jan Kaluza Date: Wed, 24 Feb 2016 08:35:10 +0100 Subject: [PATCH] Libtransport: Fix memory leak in HTTPRequest, initialize CURL in the same thread as we use for its cleanup --- libtransport/HTTPRequest.cpp | 20 +++++++++++++++----- libtransport/ThreadPool.cpp | 4 ++-- tests/libtransport/main.cpp | 5 +++++ 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/libtransport/HTTPRequest.cpp b/libtransport/HTTPRequest.cpp index d9b30bdb..90c0bfb9 100644 --- a/libtransport/HTTPRequest.cpp +++ b/libtransport/HTTPRequest.cpp @@ -9,16 +9,14 @@ HTTPRequest::HTTPRequest(ThreadPool *tp, Type type, const std::string &url, Call m_url = url; m_tp = tp; m_callback = callback; - - init(); + curlhandle = NULL; } HTTPRequest::HTTPRequest(Type type, const std::string &url) { m_type = type; m_url = url; m_tp = NULL; - - init(); + curlhandle = NULL; } HTTPRequest::~HTTPRequest() { @@ -30,9 +28,12 @@ HTTPRequest::~HTTPRequest() { } bool HTTPRequest::init() { + if (curlhandle) { + return true; + } + curlhandle = curl_easy_init(); if (curlhandle) { - curlhandle = curl_easy_init(); curl_easy_setopt(curlhandle, CURLOPT_PROXY, NULL); curl_easy_setopt(curlhandle, CURLOPT_PROXYUSERPWD, NULL); curl_easy_setopt(curlhandle, CURLOPT_PROXYAUTH, (long)CURLAUTH_ANY); @@ -113,11 +114,19 @@ bool HTTPRequest::GET(std::string url, rapidjson::Document &json) { } void HTTPRequest::run() { + if (!init()) { + m_ok = false; + return; + } + switch (m_type) { case Get: m_ok = GET(m_url, m_json); break; } + + curl_easy_cleanup(curlhandle); + curlhandle = NULL; } void HTTPRequest::finalize() { @@ -135,6 +144,7 @@ bool HTTPRequest::execute() { } bool HTTPRequest::execute(rapidjson::Document &json) { + init(); switch (m_type) { case Get: m_ok = GET(m_url, json); diff --git a/libtransport/ThreadPool.cpp b/libtransport/ThreadPool.cpp index 6a601eb4..6ffdea50 100644 --- a/libtransport/ThreadPool.cpp +++ b/libtransport/ThreadPool.cpp @@ -18,7 +18,7 @@ ThreadPool::ThreadPool(Swift::EventLoop *loop, int maxthreads) : MAX_THREADS(max { this->loop = loop; activeThreads = 0; - worker = new boost::thread*[MAX_THREADS]; + worker = (boost::thread **) malloc(sizeof(boost::thread *) * MAX_THREADS); for(int i=0 ; i