Libtransport: Fix memory leak in HTTPRequest, initialize CURL in the same thread as we use for its cleanup

This commit is contained in:
Jan Kaluza 2016-02-24 08:35:10 +01:00
parent 1e48fe7719
commit 7500ab6c4c
3 changed files with 22 additions and 7 deletions

View file

@ -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);

View file

@ -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<MAX_THREADS ; i++) {
worker[i] = NULL;
freeThreads.push(i);
@ -34,7 +34,7 @@ ThreadPool::~ThreadPool()
delete worker[i];
}
}
delete worker;
free(worker);
while(!requestQueue.empty()) {
Thread *t = requestQueue.front(); requestQueue.pop();

View file

@ -12,6 +12,7 @@
#include "log4cxx/propertyconfigurator.h"
#include "transport/protocol.pb.h"
#include "transport/HTTPRequest.h"
using namespace log4cxx;
#endif
@ -38,6 +39,8 @@ int main (int argc, char* argv[])
testsToRun.push_back("");
}
Transport::HTTPRequest::globalInit();
// informs test-listener about testresults
CPPUNIT_NS :: TestResult testresult;
@ -58,6 +61,7 @@ int main (int argc, char* argv[])
}
catch (const std::exception& e) {
google::protobuf::ShutdownProtobufLibrary();
Transport::HTTPRequest::globalCleanup();
std::cerr << "Error: " << e.what() << std::endl;
return -1;
}
@ -68,6 +72,7 @@ int main (int argc, char* argv[])
compileroutputter.write ();
google::protobuf::ShutdownProtobufLibrary();
Transport::HTTPRequest::globalCleanup();
// return 0 if tests were successful
return collectedresults.wasSuccessful () ? 0 : 1;