-
-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix arguments of dispatched functions cannot be actually moved #7873
base: RC_2_0
Are you sure you want to change the base?
Conversation
Oh, damn it! |
40be6c3
to
3e1b699
Compare
3e1b699
to
fe0506b
Compare
145f938
to
1c465b3
Compare
dd85995
to
fceba8d
Compare
src/session_handle.cpp
Outdated
@@ -92,12 +93,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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that trick with std::bind
isn't required since c++20. It can be rewritten as the following:
dispatch(s->get_context(), [=, ...args = std::forward<Args>(a)]() mutable
{
// do something
});
@@ -928,11 +928,12 @@ void test_fastresume(bool const test_deprecated) | |||
p.storage_mode = storage_mode_sparse; | |||
error_code ignore; | |||
torrent_handle h = ses.add_torrent(std::move(p), ignore); | |||
TEST_CHECK(exists(combine_path(p.save_path, "temporary"))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is noteworthy that this "use-after-move" bug also went unnoticed, as p
was not actually moved.
fceba8d
to
ab89194
Compare
@@ -92,12 +93,12 @@ | |||
{ | |||
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([s, f](auto&&... args) mutable |
Check notice
Code scanning / CodeQL
Unused static variable Note
@@ -92,12 +93,12 @@ | |||
{ | |||
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([s, f](auto&&... args) mutable |
Check notice
Code scanning / CodeQL
Unused local variable Note
@@ -92,12 +93,12 @@ | |||
{ | |||
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([s, f](auto&&... args) mutable |
Check notice
Code scanning / CodeQL
Declaration hides parameter Note
parameter of the same name
@@ -154,12 +155,12 @@ | |||
bool done = false; | |||
Ret r; | |||
std::exception_ptr ex; | |||
dispatch(s->get_context(), [=, &r, &done, &ex]() mutable | |||
dispatch(s->get_context(), std::bind([s, f, &r, &done, &ex](auto&&... args) mutable |
Check notice
Code scanning / CodeQL
Unused static variable Note
@@ -154,12 +155,12 @@ | |||
bool done = false; | |||
Ret r; | |||
std::exception_ptr ex; | |||
dispatch(s->get_context(), [=, &r, &done, &ex]() mutable | |||
dispatch(s->get_context(), std::bind([s, f, &r, &done, &ex](auto&&... args) mutable |
Check notice
Code scanning / CodeQL
Unused local variable Note
@@ -122,12 +123,12 @@ | |||
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([t, f, &ses](auto&&... args) mutable |
Check notice
Code scanning / CodeQL
Unused local variable Note
@@ -122,12 +123,12 @@ | |||
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([t, f, &ses](auto&&... args) mutable |
Check notice
Code scanning / CodeQL
Declaration hides parameter Note
parameter of the same name
@@ -190,12 +191,12 @@ | |||
bool done = false; | |||
|
|||
std::exception_ptr ex; | |||
dispatch(ses.get_context(), [=,&r,&done,&ses,&ex] () | |||
dispatch(ses.get_context(), std::bind([t, f, &r, &done, &ses, &ex](auto&&... args) mutable |
Check notice
Code scanning / CodeQL
Unused static variable Note
@@ -190,12 +191,12 @@ | |||
bool done = false; | |||
|
|||
std::exception_ptr ex; | |||
dispatch(ses.get_context(), [=,&r,&done,&ses,&ex] () | |||
dispatch(ses.get_context(), std::bind([t, f, &r, &done, &ses, &ex](auto&&... args) mutable |
Check notice
Code scanning / CodeQL
Unused local variable Note
@@ -190,12 +191,12 @@ | |||
bool done = false; | |||
|
|||
std::exception_ptr ex; | |||
dispatch(ses.get_context(), [=,&r,&done,&ses,&ex] () | |||
dispatch(ses.get_context(), std::bind([t, f, &r, &done, &ses, &ex](auto&&... args) mutable |
Check notice
Code scanning / CodeQL
Declaration hides parameter Note
parameter of the same name
ab89194
to
8ec5ee7
Compare
8ec5ee7
to
cc0b207
Compare
@@ -92,12 +93,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([s, f](auto&&... args) mutable |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be beneficial to move function f
into the lambda too if possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be beneficial to move function
f
into the lambda too if possible.
Generally, yes. But in this case, it is intended only for pointer to member function, so moving it will not add any advantages.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. So it is a pointer rather than a function object...
@@ -92,12 +93,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([s, f](auto&&... args) mutable |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not:
dispatch(s->get_context(), std::bind([s, f](auto&&... args) mutable | |
dispatch(s->get_context(), std::bind([s, f](Args&&... args) mutable |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not:
It is incorrect.
It seems that move-only arguments were never used, so the problem went unnoticed. I discovered it when I was experimenting with
boost::promise
.