简体   繁体   English

为什么删除操作符会在调试模式下导致调试断言失败?

[英]Why does delete operator causes Debug Assertion Fail in Debug Mode?

Recently I noticed that my Engine is absolutely unoptimized so i decide to do a little cleanup. 最近,我注意到我的引擎绝对未优化,因此我决定进行一些清理。 After implementing some of the culling algorithms I open Windows task manager to see how much my application uses memory. 在实现了某些筛选算法之后,我打开Windows任务管理器以查看我的应用程序使用了多少内存。 In result of my short research about "new" and "delete" operators I tried to use them for more efficiently memory usage. 通过对“ new”和“ delete”运算符的简短研究,我尝试使用它们来更有效地使用内存。 For example: 例如:

void DrawSkybox()
{
p_Device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE );
D3DXVECTOR3* Scal = new D3DXVECTOR3(1.5f, 1.5f, 1.5f);
D3DXVECTOR3* Rot = new D3DXVECTOR3(0, 0, 0);
obj_Skybox->Transform(&vec3_camera_viewPos, Scal, Rot);
DrawObject(p_Device, obj_Skybox, NULL, NULL, NULL, false);
p_Device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE );
delete Scal;
delete Rot;
}

In the first time I compiled and executed debug version of Engine I received an error: "Debug Assertion Failed". 在我第一次编译并执行Engine的调试版本时,我收到一个错误:“调试断言失败”。 Then I compiled and executed my Engine as a Release - everything is just fine. 然后,我作为发行版编译并执行了我的引擎-一切都很好。 So what is the problem? 那是什么问题呢?

You better should refactor your code in this way, instead of bothering about new and delete : 您最好应该以这种方式重构代码,而不是为newdelete烦恼:

void DrawSkybox() {
     p_Device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE );
     D3DXVECTOR3 Scal(1.5f, 1.5f, 1.5f); // <<<< Just use stack 
     D3DXVECTOR3 Rot (0, 0, 0);          // <<<< allocated variables ...
     obj_Skybox->Transform(&vec3_camera_viewPos, &Scal, &Rot); // <<< ... and pass 
                                                               // their addresses
     DrawObject(p_Device, obj_Skybox, NULL, NULL, NULL, false);
     p_Device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE );
}

I can't tell anymore details about that actual assertion failure you get, but it's occuring in debug mode only, and release mode compiled code won't notice what's going on. 我再也无法告诉您有关实际断言失败的详细信息,但是它仅在调试模式下发生,而发布模式下的编译代码不会注意到发生了什么。
The errors seem to stem from something different in your code elsewhere, but shouldn't from just introducing the delete statements. 错误似乎源于其他地方的代码中的某些不同之处,但不应该源于仅引入delete语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM