-
Notifications
You must be signed in to change notification settings - Fork 2.2k
When determining if a shared_ptr already exists, use a test on the we… #2819
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
When determining if a shared_ptr already exists, use a test on the we… #2819
Conversation
…ak_ptr instead of a try/catch block.
YannickJadoul
left a comment
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.
Thanks! Looks good to me!
Seems like this isn't even an improvement to avoid try-catch, but even a fix that avoids UB on C++<17?
Quoting https://en.cppreference.com/w/cpp/memory/enable_shared_from_this/shared_from_this
It is permitted to call
shared_from_thisonly on a previously shared object, i.e. on an object managed bystd::shared_ptr(in particular,shared_from_thiscannot be called during construction of*this).Otherwise the behavior is undefined (until C++17)
std::bad_weak_ptris thrown (by theshared_ptrconstructor from a default-constructedweak_this) (since C++17).
There's no such note for weak_from_this: https://en.cppreference.com/w/cpp/memory/enable_shared_from_this/weak_from_this
Do you happen to know if this is now completely safe?
|
Sadly |
…ak_ptr instead of a try/catch block.
…1 into exception_free_shared_ptr
rwgk
left a comment
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.
This PR was run through the Google global testing (by the author) and is already in the production environment.
|
My attempts to add Microsoft-specific checks seem to have failed, for reasons which are obscure to me. |
|
The one failed test is not to worry about; it's one we sometimes see and hasn't been fixed yet. Unrelated to this PR. |
ac9dd31 to
651fd41
Compare
|
Refactored out the function (so we only need to write this condition once) and added the One more thing, @elkhrt: what was your reason for this change? According to cppreference, we still have UB in C++ versions < C++17, and the try-catch was already non-UB in C++17 and later. So I'm not sure what's the advantage that justifies partially duplicating this code? |
rwgk
left a comment
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.
Awesome, thanks @YannickJadoul!
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.
Thanks, @rwgk. Pushed another commit with this fixed. One more question to resolve. If it's up to me, I'd still simplify the code (and ironically +- end up where we started).
|
Green, green, green, green, ... Thanks, @elkhrt! |
…ak_ptr instead of a try/catch block.
Description
EDIT (@YannickJadoul): Quoting #2819 (comment)
Suggested changelog entry: