Initializer_list calls object destructor twice - by Agounaro

Status : 

  Fixed<br /><br />
		This item has been fixed in the current or upcoming version of this product.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.


6
0
Sign in
to vote
ID 800364 Comments
Status Closed Workarounds
Type Bug Repros 3
Opened 9/12/2013 6:34:57 AM
Access Restriction Public

Description

First C++ object in an Initializer list created with the new { {..}, {...} } syntax will have its destructor called twice. This results in the destructor running on an already destroyed object, often causing a crash
Sign in to post a comment.
Posted by ichramm on 5/22/2014 at 8:49 PM
Fixed in Visual Studio 2013 Update 2
Posted by ichramm on 1/31/2014 at 6:48 PM
Still happening after installing the Update 1 for Visual Studio 2013.

But it happens only with nested initializer list.

The following is a minimal example about how to reproduce the issue.

A more complete example can be found in the attached file nested_initializer_list.cpp

#include <map>
#include <string>
#include <vector>
using namespace std;
class test
{
public:
    string _name;
    map<string, test> _childs;
    test(const char *name)
        : _name(name) { }
    test(const string& name)
        : _name(name) { }
    test(initializer_list<std::pair<const string, test>> childs)
        : _name("composite" + to_string(++compositesCount))
    {
        _childs.insert(childs);
    }
    test(const test& o)
        : _name(o._name), _childs(o._childs) { }
    test(test&& o)
        : _name(move(o._name)), _childs(move(o._childs)) { }
    ~test() { }
};

int main() {
    test t{
        { "one", "is 1" },
        { "two", "is 2" },
        { "three", {
            { "threeDotOne", "is 3.1" },
            { "threeDotTwo", "is 3.2" }
        }},
        { "four", "is 4" },
        { "five", "is 5" },
    };
}
Posted by Microsoft on 11/25/2013 at 2:37 PM
Hi:
    Thanks for reporting the issue.
    A fix for this issue has been checked into the compiler sources. The fix should show up in the future release of Visual C++.

Xiang Fan
Visual C++ Team
Posted by Jasperb00 on 11/24/2013 at 2:22 AM
I got the same problem here.
Posted by UnitUniverse on 10/30/2013 at 1:19 PM
okay I've tried to make a workarounds of this and found that it's no way to make it right. The initializer_list is compiler integrated recognition and the wrapped code wouldn't be accepted by the compiler. I tried to use the variadic template but it asked the Print function to be modified first. So if it won't be fixed soon then the initializer_list in v120 became unusable at all because you never know when the generated binary would be right or wrong.
Posted by Microsoft on 9/12/2013 at 7:46 PM
Thank you for submitting feedback on Visual Studio and .NET Framework. Your issue has been routed to the appropriate VS development team for investigation. We will contact you if we require any additional information.
Posted by Microsoft on 9/12/2013 at 6:50 AM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)