I have a piece of code like this:
class Data
{
public:
Data(const std::vector<int> &_data)
{
my_data = _data;
}
private:
std::vector<int> my_data;
};
int main()
{
std::vector<std::shared_ptr<Data>> vec = {
std::shared_ptr<Data>(new Data(std::vector<int>({ 1, 2 ,3 }))),
std::shared_ptr<Data>(new Data(std::vector<int>({ 3, 4 ,5 })))
};
// breakpoint
return 0;
}
somehow when I pause the program to check values (at breakpoint), the first (vec[0]
) element is destroyed while the second one (vec[1]
) is fine. What is going on here? Is that a bug in compiler? I am using new Visual Studio 2013.
What happens is that a bug in VS2013 causes a double delete on the first item of the initializer_list. Here's the flow:
delete[]
). Last element is destroyed first.delete
).I've seen this on another post and verified the behavior using a debugger. See here
For VS2013, initializer_list is good for basic types only.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With