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

<memory>: atomic smart pointers do not work with array elements #1332

Closed
xmas92 opened this issue Oct 1, 2020 · 3 comments · Fixed by #1339
Closed

<memory>: atomic smart pointers do not work with array elements #1332

xmas92 opened this issue Oct 1, 2020 · 3 comments · Fixed by #1339
Labels
bug Something isn't working fixed Something works now, yay!

Comments

@xmas92
Copy link
Contributor

xmas92 commented Oct 1, 2020

Describe the bug
atomic<shared_ptr<Ty>> and atomic<weak_ptr<Ty>> does not work when Ty is and an array { Ty = T[] || T[N] }.
No sure if this is intended. [util.smartptr.atomic] does not really specify.

Code test case
Godbolt example (Uncomment lines)

#include <memory>

int main() {
    using namespace std;
    shared_ptr<int> sptr0;
    shared_ptr<int[]> sptr1;
    shared_ptr<int[2]> sptr2;
    
    atomic<shared_ptr<int>> atomic_sptr0;
    atomic<shared_ptr<int[]>> atomic_sptr1;
    atomic<shared_ptr<int[2]>> atomic_sptr2;

    atomic_sptr0 = sptr0;
    atomic_sptr1 = sptr1;                                      // <-- Broken ?
    atomic_sptr2 = sptr2;                                      // <-- Broken ?
    
    weak_ptr<int> wptr0;
    weak_ptr<int[]> wptr1;
    weak_ptr<int[2]> wptr2;
    
    atomic<weak_ptr<int>> atomic_wptr0;
    atomic<weak_ptr<int[]>> atomic_wptr1;
    atomic<weak_ptr<int[2]>> atomic_wptr2;
    
    atomic_wptr0 = wptr0;
    atomic_wptr1 = wptr1;                                      // <-- Broken ?
    atomic_wptr2 = wptr2;                                      // <-- Broken ?

    return 0;
}

Expected behavior
For the code to be compiled.

STL version
All of them but locally I ran into the problem with:
Microsoft Visual Studio Community 2019 Preview Version 16.8.0 Preview 3.2
Additional context
The issue seems come from a mismatch between the type used to represent the internal pointer in shared_ptr and weak_ptr compared to atomic<x_ptr<>> . shared_ptr<T>::element_type and weak_ptr<T>::element_type is of type remove_extent_t<T> while atomic<x_ptr<T>> internal representation is just T*. So it ends up trying to perform operations between T[] * and T*

I implemented my own fix (0fd8850) so I could continue with my projects.
Simply changed:

- _Ty*
+ remove_extent_t<_Ty>*

Also extended the one test suit (975ac24) as a sanity check.

(Does not test wait, notify_one and notify_all)

@StephanTLavavej StephanTLavavej added the bug Something isn't working label Oct 2, 2020
@StephanTLavavej
Copy link
Member

Thanks, this does seem like a bug to me. The atomic machinery should be completely orthogonal to the pointed-to object/array, but we just forgot about this potential interaction 🙀

@miscco
Copy link
Contributor

miscco commented Oct 2, 2020

If you already implemented a fix could you issue a PR for that, given that you already have the commits ready?

@xmas92
Copy link
Contributor Author

xmas92 commented Oct 2, 2020

Done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed Something works now, yay!
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants