[英]Destructor called without copy or move constructor during array initialization on MSVC
I have tried this on MSVC versions 19.10.25019.0 and 19.11.25547.0 in debug and release builds and get the same result. 我已在调试和发行版本的MSVC版本19.10.25019.0和19.11.25547.0上尝试过此操作,并获得相同的结果。
The following program prints 0 1 2 3 4 5
. 以下程序打印
0 1 2 3 4 5
。 I expected either 我期望
Instead, it seems the destructor was called for 6 of the 7 elements and no copy or move constructor calls were made. 相反,似乎在7个元素中的6个调用了析构函数,并且没有进行复制或移动构造函数调用。
#include <iostream>
struct MyChar {
MyChar(char c) : c{c}{}
MyChar() = default;
MyChar(const MyChar&) { std::cout << "copy"; };
MyChar& operator=(const MyChar&) { std::cout << "assign"; return *this; };
MyChar(MyChar&&) { std::cout << "move"; };
MyChar& operator=(const MyChar&&) { std::cout << "move assign"; return *this; };
~MyChar() { std::cout << cnt++ << '\t'; }
char c{'H'};
static int cnt;
};
int MyChar::cnt{};
int main()
{
auto arr1 = new MyChar[7]{'D'};
}
Why is the destructor being called 6 times without delete (or without the compiler initializing via a copy or a move)? 为什么析构函数被调用6次而没有删除(或者没有通过复制或移动初始化编译器)?
In playing around with this in the VC compiler, and inspecting the generated assembly code, this appears to be a compiler bug related to the MyChar() = default
constructor. 在VC编译器中解决此问题并检查生成的汇编代码时,这似乎是与
MyChar() = default
构造函数有关的编译器错误。 Replacing it with MyChar() { }
gets rid of the unexpected destructor calls. 用
MyChar() { }
替换它可以摆脱意外的析构函数调用。
By adding some additional code into the destructor, the objects being destroyed are the default-initialized members of arr1
. 通过在析构函数中添加一些其他代码,被销毁的对象是
arr1
的默认初始化成员。 Adding in a call to delete [] arr1
, along with including the address of the object being destroyed in the destructor, shows that the first element is destroyed once, while the other 6 are destroyed twice - once when arr1
is constructed, and again when the delete call is made. 加上对
delete [] arr1
的调用,并在析构函数中包含要破坏的对象的地址,表明第一个元素被破坏了一次,而其他6个被破坏了两次-一次在构造arr1
时被破坏,一次在以下情况下被破坏:进行删除呼叫。
This should be reported to Microsoft. 这应该报告给Microsoft。 It occurs in both VC2015 and VC2017.
它同时在VC2015和VC2017中出现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.