Skip to content
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

Avoid resetting unique_ptr not being the owner of the pointed element #1086

Closed

Conversation

whoan
Copy link
Contributor

@whoan whoan commented Mar 26, 2019

This PR is to avoid resetting a unique_ptr in a new thread as it (the new thread) is not the owner of the pointed element, and unique_ptr is not thread safe, so some obscure errors could happen. Consider this snippet for testing purposes:

#include <memory>
#include <thread>

int main() {
  std::unique_ptr<int> unique(new int);
  std::thread thread([&unique] () {
    unique.reset();
  });
  unique.reset();
  thread.join();
  return 0;
}

Both unique.reset(); might be called at the same time causing UB. I think something similar is happening in shutdown_wspp_impl.

What is the price of stop calling m_client.reset(); as suggested in this PR?

This might be related to issue #32

@whoan whoan changed the title Avoid resetting unique_ptr not being the owner of the pointer Avoid resetting unique_ptr not being the owner of the pointed element Mar 26, 2019
@BillyONeal
Copy link
Member

Hmmm I don't think this is the case because it looks like the thread is joined before the call to reset, not after. This change doesn't seem to do anything other than consume more memory?

@whoan
Copy link
Contributor Author

whoan commented Mar 26, 2019

@BillyONeal I have seen some double deletions of the unique_ptr but I wonder if it was before PR #1080 was joined. I will try to reproduce later and will let you know.

@BillyONeal
Copy link
Member

OK, feel free to reopen if you can run it down.

@BillyONeal BillyONeal closed this Mar 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants