Uh oh!
There was an error while loading. Please reload this page.
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.
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?
elkhrt
commented
Jan 25, 2021
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.
This PR was run through the Google global testing (by the author) and is already in the production environment.
Uh oh!
There was an error while loading. Please reload this page.
elkhrt
commented
Jan 30, 2021
My attempts to add Microsoft-specific checks seem to have failed, for reasons which are obscure to me. |
YannickJadoul
commented
Jan 30, 2021
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
651fd41CompareYannickJadoul
commented
Jan 30, 2021
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.
Awesome, thanks @YannickJadoul!
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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).
Uh oh!
There was an error while loading. Please reload this page.
YannickJadoul
commented
Jan 30, 2021
Green, green, green, green, ... Thanks, @elkhrt! |
…ak_ptr instead of a try/catch block.
Description
EDIT (@YannickJadoul): Quoting #2819 (comment)
Suggested changelog entry: