Skip to content

Commit 4fe9ed9

Browse files
authored
Merge pull request #144 from mujin/fixFormFree20231120
Fix memory leak by incorrect usage of curl_formfree
2 parents abb1ece + b6c08c0 commit 4fe9ed9

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/controllerclientimpl.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,15 @@ MUJIN_LOGGER("mujin.controllerclientcpp");
3131
#define CURL_OPTION_SAVE_SETTER(curl, curlopt, curvalue, newvalue) CURL_OPTION_SAVER(curl, curlopt, curvalue); CURL_OPTION_SETTER(curl, curlopt, newvalue)
3232
#define CURL_INFO_GETTER(curl, curlinfo, outvalue) CHECKCURLCODE(curl_easy_getinfo(curl, curlinfo, outvalue), "curl_easy_getinfo " # curlinfo)
3333
#define CURL_PERFORM(curl) CHECKCURLCODE(curl_easy_perform(curl), "curl_easy_perform")
34-
#define CURL_FORM_RELEASER(form) boost::shared_ptr<void> __curlformreleaser ## form((void*)0, boost::bind(boost::function<void(decltype(form))>(curl_formfree), form))
34+
35+
struct CURLFormReleaser {
36+
struct curl_httppost *& form;
37+
38+
~CURLFormReleaser()
39+
{
40+
curl_formfree(std::exchange(form, nullptr));
41+
}
42+
};
3543

3644
namespace mujinclient {
3745

@@ -1910,7 +1918,7 @@ void ControllerClientImpl::_UploadFileToControllerViaForm(std::istream& inputStr
19101918
// prepare form
19111919
struct curl_httppost *formpost = NULL;
19121920
struct curl_httppost *lastptr = NULL;
1913-
CURL_FORM_RELEASER(formpost);
1921+
CURLFormReleaser curlFormReleaser{formpost};
19141922
curl_formadd(&formpost, &lastptr,
19151923
CURLFORM_COPYNAME, "files[]",
19161924
CURLFORM_FILENAME, filename.empty() ? "unused" : filename.c_str(),
@@ -1958,7 +1966,7 @@ void ControllerClientImpl::_UploadDataToControllerViaForm(const void* data, size
19581966
// prepare form
19591967
struct curl_httppost *formpost = NULL;
19601968
struct curl_httppost *lastptr = NULL;
1961-
CURL_FORM_RELEASER(formpost);
1969+
CURLFormReleaser curlFormReleaser{formpost};
19621970
curl_formadd(&formpost, &lastptr,
19631971
CURLFORM_PTRNAME, "files[]",
19641972
CURLFORM_BUFFER, filename.empty() ? "unused" : filename.c_str(),
@@ -2091,7 +2099,7 @@ void ControllerClientImpl::CreateLogEntries(const std::vector<LogEntryPtr>& logE
20912099
// prepare the form
20922100
struct curl_httppost *formpost = NULL;
20932101
struct curl_httppost *lastptr = NULL;
2094-
CURL_FORM_RELEASER(formpost);
2102+
CURLFormReleaser curlFormReleaser{formpost};
20952103

20962104
rapidjson::StringBuffer& rRequestStringBuffer = _rRequestStringBufferCache;
20972105
rapidjson::Writer<rapidjson::StringBuffer> writer(rRequestStringBuffer);

0 commit comments

Comments
 (0)