Skip to content

Commit

Permalink
Fix arguments of dispatched functions cannot be actually moved
Browse files Browse the repository at this point in the history
  • Loading branch information
glassez committed Feb 16, 2025
1 parent c31d90b commit ea1aa43
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 27 deletions.
24 changes: 11 additions & 13 deletions src/session_handle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,12 @@ namespace libtorrent {
{
std::shared_ptr<session_impl> s = m_impl.lock();
if (!s) aux::throw_ex<system_error>(errors::invalid_session_handle);
dispatch(s->get_context(), [=]() mutable
dispatch(s->get_context(), std::bind([=](auto&&... args) mutable
{
#ifndef BOOST_NO_EXCEPTIONS
try {
#endif
(s.get()->*f)(std::forward<Args>(a)...);
(s.get()->*f)(std::forward<Args>(args)...);
#ifndef BOOST_NO_EXCEPTIONS
} catch (system_error const& e) {
s->alerts().emplace_alert<session_error_alert>(e.code(), e.what());
Expand All @@ -107,7 +107,7 @@ namespace libtorrent {
s->alerts().emplace_alert<session_error_alert>(error_code(), "unknown error");
}
#endif
});
}, std::forward<Args>(a)...));
}

template<typename Fun, typename... Args>
Expand All @@ -122,12 +122,12 @@ namespace libtorrent {
bool done = false;

std::exception_ptr ex;
dispatch(s->get_context(), [=, &done, &ex]() mutable
dispatch(s->get_context(), std::bind([=, &done, &ex](auto&&... args) mutable
{
#ifndef BOOST_NO_EXCEPTIONS
try {
#endif
(s.get()->*f)(std::forward<Args>(a)...);
(s.get()->*f)(std::forward<Args>(args)...);
#ifndef BOOST_NO_EXCEPTIONS
} catch (...) {
ex = std::current_exception();
Expand All @@ -136,7 +136,7 @@ namespace libtorrent {
std::unique_lock<std::mutex> l(s->mut);
done = true;
s->cond.notify_all();
});
}, std::forward<Args>(a)...));

aux::torrent_wait(done, *s);
if (ex) std::rethrow_exception(ex);
Expand All @@ -154,12 +154,12 @@ namespace libtorrent {
bool done = false;
Ret r;
std::exception_ptr ex;
dispatch(s->get_context(), [=, &r, &done, &ex]() mutable
dispatch(s->get_context(), std::bind([=, &r, &done, &ex](auto&&... args) mutable
{
#ifndef BOOST_NO_EXCEPTIONS
try {
#endif
r = (s.get()->*f)(std::forward<Args>(a)...);
r = (s.get()->*f)(std::forward<Args>(args)...);
#ifndef BOOST_NO_EXCEPTIONS
} catch (...) {
ex = std::current_exception();
Expand All @@ -168,7 +168,7 @@ namespace libtorrent {
std::unique_lock<std::mutex> l(s->mut);
done = true;
s->cond.notify_all();
});
}, std::forward<Args>(a)...));

aux::torrent_wait(done, *s);
if (ex) std::rethrow_exception(ex);
Expand Down Expand Up @@ -426,8 +426,7 @@ namespace {
handle_backwards_compatible_resume_data(params);
#endif
error_code ec;
auto ecr = std::ref(ec);
torrent_handle r = sync_call_ret<torrent_handle>(&session_impl::add_torrent, std::move(params), ecr);
torrent_handle r = sync_call_ret<torrent_handle>(&session_impl::add_torrent, std::move(params), std::ref(ec));
if (ec) aux::throw_ex<system_error>(ec);
return r;
}
Expand Down Expand Up @@ -460,8 +459,7 @@ namespace {
#if TORRENT_ABI_VERSION == 1
handle_backwards_compatible_resume_data(params);
#endif
auto ecr = std::ref(ec);
return sync_call_ret<torrent_handle>(&session_impl::add_torrent, std::move(params), ecr);
return sync_call_ret<torrent_handle>(&session_impl::add_torrent, std::move(params), std::ref(ec));
}

torrent_handle session_handle::add_torrent(add_torrent_params const& params, error_code& ec)
Expand Down
28 changes: 14 additions & 14 deletions src/torrent_handle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,12 @@ namespace libtorrent {
std::shared_ptr<torrent> t = m_torrent.lock();
if (!t) aux::throw_ex<system_error>(errors::invalid_torrent_handle);
auto& ses = static_cast<session_impl&>(t->session());
dispatch(ses.get_context(), [=,&ses] ()
dispatch(ses.get_context(), std::bind([=, &ses](auto&&... args) mutable
{
#ifndef BOOST_NO_EXCEPTIONS
try {
#endif
(t.get()->*f)(std::move(a)...);
(t.get()->*f)(std::forward<Args>(args)...);
#ifndef BOOST_NO_EXCEPTIONS
} catch (system_error const& e) {
ses.alerts().emplace_alert<torrent_error_alert>(torrent_handle(t)
Expand All @@ -140,7 +140,7 @@ namespace libtorrent {
, error_code(), "unknown error");
}
#endif
} );
}, std::forward<Args>(a)...));
}

template<typename Fun, typename... Args>
Expand All @@ -154,12 +154,12 @@ namespace libtorrent {
bool done = false;

std::exception_ptr ex;
dispatch(ses.get_context(), [=,&done,&ses,&ex] ()
dispatch(ses.get_context(), std::bind([=, &done, &ses, &ex](auto&&... args) mutable
{
#ifndef BOOST_NO_EXCEPTIONS
try {
#endif
(t.get()->*f)(std::move(a)...);
(t.get()->*f)(std::forward<Args>(args)...);
#ifndef BOOST_NO_EXCEPTIONS
} catch (...) {
ex = std::current_exception();
Expand All @@ -168,7 +168,7 @@ namespace libtorrent {
std::unique_lock<std::mutex> l(ses.mut);
done = true;
ses.cond.notify_all();
} );
}, std::forward<Args>(a)...));

aux::torrent_wait(done, ses);
if (ex) std::rethrow_exception(ex);
Expand All @@ -190,12 +190,12 @@ namespace libtorrent {
bool done = false;

std::exception_ptr ex;
dispatch(ses.get_context(), [=,&r,&done,&ses,&ex] ()
dispatch(ses.get_context(), std::bind([=, &r, &done, &ses, &ex](auto&&... args) mutable
{
#ifndef BOOST_NO_EXCEPTIONS
try {
#endif
r = (t.get()->*f)(std::move(a)...);
r = (t.get()->*f)(std::forward<Args>(args)...);
#ifndef BOOST_NO_EXCEPTIONS
} catch (...) {
ex = std::current_exception();
Expand All @@ -204,7 +204,7 @@ namespace libtorrent {
std::unique_lock<std::mutex> l(ses.mut);
done = true;
ses.cond.notify_all();
} );
}, std::forward<Args>(a)...));

aux::torrent_wait(done, ses);

Expand Down Expand Up @@ -511,8 +511,8 @@ namespace libtorrent {

void torrent_handle::piece_availability(std::vector<int>& avail) const
{
auto availr = std::ref(static_cast<aux::vector<int, piece_index_t>&>(avail));
sync_call(&torrent::piece_availability, availr);
auto& arg = static_cast<aux::vector<int, piece_index_t>&>(avail);
sync_call(&torrent::piece_availability, std::ref(arg));
}

void torrent_handle::piece_priority(piece_index_t index, download_priority_t priority) const
Expand Down Expand Up @@ -641,7 +641,8 @@ namespace libtorrent {
#if !TORRENT_NO_FPU
void torrent_handle::file_progress(std::vector<float>& progress) const
{
sync_call(&torrent::file_progress_float, std::ref(static_cast<aux::vector<float, file_index_t>&>(progress)));
auto& arg = static_cast<aux::vector<float, file_index_t>&>(progress);
sync_call(&torrent::file_progress_float, std::ref(arg));
}
#endif

Expand Down Expand Up @@ -796,8 +797,7 @@ namespace libtorrent {
entry torrent_handle::write_resume_data() const
{
add_torrent_params params;
auto retr = std::ref(params);
sync_call(&torrent::write_resume_data, resume_data_flags_t{}, retr);
sync_call(&torrent::write_resume_data, resume_data_flags_t{}, std::ref(params));
return libtorrent::write_resume_data(params);
}

Expand Down

0 comments on commit ea1aa43

Please sign in to comment.