HEAP: Free Heap block XXXX modified at XXXX after it was freed
開發過程中,有時候會遇到堆異常的情況
這時,VS的調試輸出窗口會提示:
HEAP[MemTest.exe]: HEAP: Free Heap block 39b998 modified at 39b9c0 after it was freed
Windows has triggered a breakpoint in MemTest.exe.
This may be due to a corruption of the heap, which indicates a bug in MemTest.exe or any of the DLLs it has loaded.
This may also be due to the user pressing F12 while MemTest.exe has focus.
The output window may have more diagnostic information.
就是說已經釋放的堆內存被修改了,一般就是new/malloc 后的內存,在被delete/free后又被使用了
一個簡單例子
#include "stdafx.h" #include <stdlib.h> #include <string.h> void test(void); void testStruct(void); void tt(void); struct tagTest { int a; int b; }; int _tmain(int argc, _TCHAR* argv[]) { testStruct(); test(); system("pause"); return 0; } void test(void) { char* pBuffer = new char[128]; strcpy(pBuffer, "hello"); printf("%p %s\n", pBuffer, pBuffer); delete[] pBuffer; strcpy(pBuffer, "HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH"); printf("%p %s\n", pBuffer, pBuffer); tt(); } void tt(void) { printf("I do nothing\n"); } void testStruct(void) { tagTest* pTest = new tagTest(); pTest->a = 0x11111111; pTest->b = 0x22222222; printf("%p %d, %d\n", pTest, pTest->a, pTest->b); delete pTest; pTest->a = 0x33333333; pTest->b = 0x44444444; printf("%p %d, %d\n", pTest, pTest->a, pTest->b); tt(); }
VC2008編譯運行以上程序(DEBUG模式),發現運行到void test(void)函數的new那條語句時程序報錯
不過這個new是不應該報此錯誤的,頂多報個內存不足就夠了......郁悶啊,后來發現,對內存被釋放后再使用是不會立即報錯的,等到再次使用堆操作函數時才會報錯。
也就是說,當時的調用堆棧是不準確的,那定位真正出問題的地方才是關鍵。
在MSDN論壇上有人說可以觀察memory窗口,根據內存內容推測問題。運氣好的話在報錯的內存地址處可以看出來蛛絲馬跡(比如說上面的實驗程序),運氣不好就只好把申請的內存和使用的地方全部打印。
Free Heap block AAAA modified at BBBB after it was freed在這句提示里,block AAAA 不是平時申請地址后的返回值,一般都比我們申請的小一些,在VC2008 DEBUG的模式下這個值比申請出來的小40(0x28)的字節, 地址 BBBB 應該總是不小于 AAAA。
這種問題不好調試,貌似網上也沒什么有效的方法,
參考:
內存窗口的使用(http://social.msdn.microsoft.com/Forums/hu-HU/vclanguage/thread/01bae812-0a5f-4b17-9745-b1c8293a25b1)
再次申請或退出時報錯(http://hi.baidu.com/lcs0000824/blog/item/9e77e4a58ef2e1ff9152ee7e.html/cmtid/783c8809d62fc19e0a7b8211)
此時堆棧不準確(http://www.davekb.com/browse_programming_tips:free_heap_block_modified_after_it_was_freed:txt)
轉載于:https://www.cnblogs.com/xkxjy/archive/2011/02/28/2078096.html
總結
以上是生活随笔為你收集整理的HEAP: Free Heap block XXXX modified at XXXX after it was freed的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据库入门浅析:ASP.NET与MySQ
- 下一篇: jquery技巧总结 学习