Skip to content

Commit

Permalink
fix memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
raquentin committed Feb 13, 2025
1 parent b02733b commit f0f8b81
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 28 deletions.
3 changes: 0 additions & 3 deletions include/curl_request.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include "curl_response.hpp"
#include "errors/curl_error.hpp"
#include <curl/curl.h>
#include <expected>
#include <string>
#include <vector>
Expand All @@ -19,8 +18,6 @@ struct CurlRequest {

std::expected<CurlResponse, CurlError> execute() const;

CURL *curl_handle_;

static size_t WriteCallback(void *contents, size_t size, size_t nmemb,
void *userp);
static size_t HeaderCallback(char *buffer, size_t size, size_t nitems,
Expand Down
38 changes: 23 additions & 15 deletions src/curl_request.cpp
Original file line number Diff line number Diff line change
@@ -1,49 +1,57 @@
#include "curl_request.hpp"
#include "curl/curl.h"
#include "errors/curl_error.hpp"
#include "printer.hpp"
#include <cstddef>
#include <curl/curl.h>
#include <curl/easy.h>

CurlRequest::CurlRequest(const std::string &file_name) : file_name_(file_name) {
curl_handle_ = curl_easy_init();
curl_easy_setopt(curl_handle_, CURLOPT_HEADERFUNCTION, HeaderCallback);
curl_easy_setopt(curl_handle_, CURLOPT_WRITEFUNCTION, WriteCallback);
};
CurlRequest::CurlRequest(const std::string &file_name)
: file_name_(file_name) {};

std::expected<CurlResponse, CurlError> CurlRequest::execute() const {
CURL *curl;

curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, HeaderCallback);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);

struct curl_slist *curl_headers = nullptr;
for (const auto &header : headers_) {
curl_headers = curl_slist_append(curl_headers, header.c_str());
}

curl_easy_setopt(curl_handle_, CURLOPT_HTTPHEADER, curl_headers);
curl_easy_setopt(curl_handle_, CURLOPT_URL, url_.c_str());
curl_easy_setopt(curl_handle_, CURLOPT_CUSTOMREQUEST, method_.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, curl_headers);
curl_easy_setopt(curl, CURLOPT_URL, url_.c_str());
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, method_.c_str());

if (!body_.empty()) {
curl_easy_setopt(curl_handle_, CURLOPT_POSTFIELDS, body_.c_str());
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, body_.c_str());
}

curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);

CurlResponse response;

curl_easy_setopt(curl_handle_, CURLOPT_WRITEDATA, &response_data_);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response_data_);

curl_easy_setopt(curl_handle_, CURLOPT_HEADERDATA, &response);
curl_easy_setopt(curl, CURLOPT_HEADERDATA, &response);

CURLcode res = curl_easy_perform(curl);

CURLcode res = curl_easy_perform(curl_handle_);
if (res != CURLE_OK) {
return std::unexpected(
CurlError(file_name_, CurlError::Type::UnitializedCurl));
}

long http_code = 0;
curl_easy_getinfo(curl_handle_, CURLINFO_RESPONSE_CODE, &http_code);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
response.parse_status_code(http_code);
response.set_body(response_data_);

curl_slist_free_all(curl_headers);
curl_easy_cleanup(curl_handle_);

curl_easy_cleanup(curl);

return response;
}
Expand Down
2 changes: 1 addition & 1 deletion src/errors/parser_error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// should all be indented
void ParserError::print() const {
std::visit(
[this](auto &&arg) {
[](auto &&arg) {
using T = std::decay_t<decltype(arg)>;

if constexpr (std::is_same_v<T, MalformedSectionHeader>) {
Expand Down
5 changes: 1 addition & 4 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include <curl/curl.h>
#include <future>
#include <memory>
#include <semaphore>
#include <string>

#include "file.hpp"
Expand Down Expand Up @@ -66,7 +65,7 @@ int main(int argc, char **argv) {
futures;

ThreadPool pool(ctx.jobs);
for (const auto filename : input_filenames.value()) {
for (const auto &filename : input_filenames.value()) {
futures.emplace_back(
pool.submit([filename] { return Raquest(filename).run(); }));
}
Expand All @@ -82,8 +81,6 @@ int main(int argc, char **argv) {
}
}

curl_global_cleanup();

if (ctx.errors_size != 0) {
printer().error_footer(ctx.errors_size);
return 1;
Expand Down
4 changes: 2 additions & 2 deletions src/thread_pool.cppm
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ export class ThreadPool {
private:
std::vector<std::thread> workers_;

/*
* @brief A
/**
* @brief A queue of function pointers, the work to be done.
*/
std::queue<std::function<void()>> tasks_;
std::mutex queue_mutex_;
Expand Down
7 changes: 4 additions & 3 deletions tests/tautology_test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <gtest/gtest.h>

TEST(Tautology, Addition) {
EXPECT_EQ(1 + 1, 2);
}
/**
* @begin Test tests.
*/
TEST(Tautology, Addition) { EXPECT_EQ(1 + 1, 2); }

0 comments on commit f0f8b81

Please sign in to comment.