这几天遇到了一个问题,很头疼。以前的一个程序,修改了部分功能后在测试中突然发现cpu很高,用鼠标操作界面也变得顿和卡了。想了各种办法都没有找到原因,今天把
_CrtSetDbgFlag的参数去掉了_CRTDBG_CHECK_ALWAYS_DF,一下子问题就解决了。
看了msdn的文档,这个标志导致频繁调用_CrtCheckMemory,导致cpu很高。msdn的文档是在这么解释的:
The behavior of _CrtCheckMemory can be controlled by setting the bit fields of the _crtDbgFlag flag using the _CrtSetDbgFlag function. Turning the _CRTDBG_CHECK_ALWAYS_DF bit field ON results in _CrtCheckMemory being called every time a memory allocation operation is requested. Although this method slows down execution, it is useful for catching errors quickly. Turning the _CRTDBG_ALLOC_MEM_DF bit field OFF causes _CrtCheckMemory to not verify the heap and immediately return TRUE.
虽然cpu很高,但是也值得,能及时检测出很多内存错误来。