std::shared_ptr in an std::initializer_list appears to be getting destroyed prematurely

后端 未结 2 928
时光取名叫无心
时光取名叫无心 2020-12-18 12:46

Edit: This is indeed caused by a bug in Visual Studio - and it has already been fixed. The issue is not reproducible after applying Update 2

2条回答
  •  不知归路
    2020-12-18 13:06

    See the original answer for analysis of object lifetimes of the code in the question. This one isolates the bug.


    I made a minimal reproduction. It's more code, but a lot less library code involved. And easier to trace.

    #include 
    
    template
    struct X
    {
        int i = N;
    
        typedef X self;
        virtual int GetValue() { return 0; }
        X()                               { std::cerr << "X<" << N << ">() default ctor" << std::endl; }
        X(const self& right) : i(right.i) { std::cerr << "X<" << N << ">(const X<" << N << "> &) copy-ctor" << std::endl; }
        X(self&& right)      : i(right.i) { std::cerr << "X<" << N << ">(X<" << N << ">&&      ) moving copy-ctor" << std::endl; }
    
        template
        X(const X& right) : i(right.i) { std::cerr << "X<" << N << ">(const X<" << M << "> &) conversion-ctor" << std::endl; }
        template
        X(X&& right)      : i(right.i) { std::cerr << "X<" << N << ">(X<" << M << ">&&      ) moving conversion-ctor" << std::endl; }
    
        ~X() { std::cerr << "~X<" << N << ">(), i = " << i << std::endl; }
    };
    
    template
    X make_X() { return X{}; }
    
    #include 
    int main()
    {
        std::initializer_list< X<0> > foo
            {
                make_X<1>(),
                make_X<2>(),
                make_X<3>(),
                make_X<4>(),
            };
    
        std::cerr << "Reached end of main" << std::endl;
    
        return 0;
    }
    

    The output is BAD on both x64:

    C:\Code\SO22924358>cl /EHsc minimal.cpp
    Microsoft (R) C/C++ Optimizing Compiler Version 18.00.21005.1 for x64
    Copyright (C) Microsoft Corporation.  All rights reserved.
    
    minimal.cpp
    Microsoft (R) Incremental Linker Version 12.00.21005.1
    Copyright (C) Microsoft Corporation.  All rights reserved.
    
    /out:minimal.exe
    minimal.obj
    
    C:\Code\SO22924358>minimal
    X<1>() default ctor
    X<0>(X<1>&&      ) moving conversion-ctor
    X<2>() default ctor
    X<0>(X<2>&&      ) moving conversion-ctor
    X<3>() default ctor
    X<0>(X<3>&&      ) moving conversion-ctor
    X<4>() default ctor
    X<0>(X<4>&&      ) moving conversion-ctor
    ~X<0>(), i = 2
    ~X<2>(), i = 2
    ~X<0>(), i = 1
    ~X<1>(), i = 1
    Reached end of main
    ~X<0>(), i = 4
    ~X<0>(), i = 3
    ~X<0>(), i = 2
    ~X<0>(), i = 1
    

    and x86:

    C:\Code\SO22924358>cl /EHsc minimal.cpp
    Microsoft (R) C/C++ Optimizing Compiler Version 18.00.21005.1 for x86
    Copyright (C) Microsoft Corporation.  All rights reserved.
    
    minimal.cpp
    Microsoft (R) Incremental Linker Version 12.00.21005.1
    Copyright (C) Microsoft Corporation.  All rights reserved.
    
    /out:minimal.exe
    minimal.obj
    
    C:\Code\SO22924358>minimal
    X<1>() default ctor
    X<0>(X<1>&&      ) moving conversion-ctor
    X<2>() default ctor
    X<0>(X<2>&&      ) moving conversion-ctor
    X<3>() default ctor
    X<0>(X<3>&&      ) moving conversion-ctor
    X<4>() default ctor
    X<0>(X<4>&&      ) moving conversion-ctor
    ~X<0>(), i = 2
    ~X<2>(), i = 2
    ~X<0>(), i = 1
    ~X<1>(), i = 1
    Reached end of main
    ~X<0>(), i = 4
    ~X<0>(), i = 3
    ~X<0>(), i = 2
    ~X<0>(), i = 1
    

    Definitely a compiler bug, and a pretty severe one. If you file a report on Connect I and many others will be happy to upvote.

提交回复
热议问题