尽早发现堆破坏

来源:互联网 发布:淘宝一付款就交易关闭 编辑:程序博客网 时间:2024/04/27 19:22

Chromium程序在运行起始位置有代码base::EnableTerminationOnHeapCorruption();


void EnableTerminationOnHeapCorruption() {
  // Ignore the result code. Supported on XP SP3 and Vista.
  HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0);
}


以上做法会让开发人员尽早的发现堆破坏。在Unmanaged EXE中尽早调用此函数,DLL中无需调用此函数。



程序中有以下代码:

const int BUFFER_SIZE = 8;LPSTR lpBuffer = new CHAR[BUFFER_SIZE];for( int i=0; i<(BUFFER_SIZE+10); i++ )// 申请了8个字节,写18个字节,堆被破坏。*(lpBuffer + i ) = 'A';delete[] lpBuffer;



如果在程序入口(App::InitInstance)加了代码HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0);,执行到以上破坏堆的代码时,程序立即退出,很容易找到出问题的代码。

原创粉丝点击