fix: memleak regressions (#2293)

* fixup! refactor: add tr_saveFile() (#2267)

fix: memleak regression

* fixup! fixup! refactor: make parts of tr file private (#2241) (#2281)

fix: memory leak regression

* fixup! refactor: use C++ inheritance for tr_peer, tr_peerMsgs, and tr_webseed (#1877)

fix: memory leak regression
This commit is contained in:
Charles Kerr 2021-12-09 18:49:30 -06:00 committed by GitHub
parent e590b1cd37
commit 0f92232c65
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 16 deletions

View File

@ -375,7 +375,7 @@ static uint64_t loadName(tr_variant* dict, tr_torrent* tor)
return 0; return 0;
} }
name = tr_strvDup(name); name = tr_strvStrip(name);
if (std::empty(name)) if (std::empty(name))
{ {
return 0; return 0;

View File

@ -1209,7 +1209,8 @@ int tr_variantToFile(tr_variant const* v, tr_variant_fmt fmt, char const* filena
{ {
auto error_code = int{ 0 }; auto error_code = int{ 0 };
auto contents_len = size_t{}; auto contents_len = size_t{};
auto const* contents = tr_variantToStr(v, fmt, &contents_len); auto* const contents = tr_variantToStr(v, fmt, &contents_len);
tr_error* error = nullptr; tr_error* error = nullptr;
tr_saveFile(filename, { contents, contents_len }, &error); tr_saveFile(filename, { contents, contents_len }, &error);
if (error != nullptr) if (error != nullptr)
@ -1218,6 +1219,8 @@ int tr_variantToFile(tr_variant const* v, tr_variant_fmt fmt, char const* filena
error_code = error->code; error_code = error->code;
tr_error_clear(&error); tr_error_clear(&error);
} }
tr_free(contents);
return error_code; return error_code;
} }

View File

@ -248,10 +248,10 @@ static void write_block_func(void* vdata)
struct connection_succeeded_data struct connection_succeeded_data
{ {
struct tr_webseed* webseed; tr_webseed* webseed = nullptr;
char* real_url; std::string real_url;
tr_piece_index_t piece_index; tr_piece_index_t piece_index = 0;
uint32_t piece_offset; uint32_t piece_offset = 0;
}; };
static void connection_succeeded(void* vdata) static void connection_succeeded(void* vdata)
@ -265,7 +265,7 @@ static void connection_succeeded(void* vdata)
w->consecutive_failures = w->retry_tickcount = w->retry_challenge = 0; w->consecutive_failures = w->retry_tickcount = w->retry_challenge = 0;
} }
if (data->real_url != nullptr) if (!std::empty(data->real_url))
{ {
tr_torrent const* const tor = tr_torrentFindFromId(w->session, w->torrent_id); tr_torrent const* const tor = tr_torrentFindFromId(w->session, w->torrent_id);
@ -275,12 +275,10 @@ static void connection_succeeded(void* vdata)
auto file_offset = uint64_t{}; auto file_offset = uint64_t{};
tr_ioFindFileLocation(tor, data->piece_index, data->piece_offset, &file_index, &file_offset); tr_ioFindFileLocation(tor, data->piece_index, data->piece_offset, &file_index, &file_offset);
w->file_urls[file_index].assign(data->real_url); w->file_urls[file_index].assign(data->real_url);
data->real_url = nullptr;
} }
} }
tr_free(data->real_url); delete data;
tr_free(data);
} }
/*** /***
@ -308,15 +306,17 @@ static void on_content_changed(struct evbuffer* buf, struct evbuffer_cb_info con
if (task->response_code == 206) if (task->response_code == 206)
{ {
auto* const data = tr_new(struct connection_succeeded_data, 1); auto const* real_url = tr_webGetTaskRealUrl(task->web_task);
data->webseed = w;
data->real_url = tr_strdup(tr_webGetTaskRealUrl(task->web_task));
data->piece_index = task->piece_index;
data->piece_offset = task->piece_offset + task->blocks_done * task->block_size + len - 1;
/* processing this uses a tr_torrent pointer, /* processing this uses a tr_torrent pointer,
so push the work to the libevent thread... */ so push the work to the libevent thread... */
tr_runInEventThread(session, connection_succeeded, data); tr_runInEventThread(
session,
connection_succeeded,
new connection_succeeded_data{ w,
real_url ? real_url : "",
task->piece_index,
task->piece_offset + task->blocks_done * task->block_size + len - 1 });
} }
} }