实例:
weapons.push_back(new Pistol());
weapons.push_back(new Rifle());
weapons.push_back(new Shotgun());
Run Code Online (Sandbox Code Playgroud)
析构函数,当第一次删除时,代码中断.当我关闭程序时会发生这种情况.
Brain::~Brain()
{
for (unsigned int i = 0; i < weapons.size(); i++)
{
delete weapons[i]; // this is where the code breaks
}
}
Run Code Online (Sandbox Code Playgroud)
我收到警告:
Unhandled exception at 0x0096371f in D3D10DEMO.exe: 0xC0000005: Access violation reading location 0x000002ce.
Run Code Online (Sandbox Code Playgroud)
武器是这样的:
weapons(vector<Gun*>())
Run Code Online (Sandbox Code Playgroud)
编辑 - 我已经删除了这个问题的大部分代码,但是我也减少了我的程序,以便在一个更小的解决方案中重现问题: