BYTE* a = new BYTE[10];
for (int idx = 0; idx <= 10; ++idx)
{
a[idx] = 1;
_ASSERTE(_CrtCheckMemory());
}
内存越界了,这样就可以及时报错了。
// 每个dll保证调用一次
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
如下面的写法:
#ifdef _WIN32
#include <crtdbg.h>
#ifdef _DEBUG
#define new new(_NORMAL_BLOCK,__FILE__,__LINE__)
#endif
#endif
#ifdef _WIN32
_CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG)|_CRTDBG_LEAK_CHECK_DF);
#endif