堆栈出错调试方法小记

这几天被一个bug搞得头疼:调试运行时一切正常,直接运行时有时候就会出错,看错误提示应该是堆栈出错,但每次出错的地

址又不一样。郁闷了好几天,最后看到网上有人说用Windows SDK里的debugging tools调试运行,果然被我抓到错误的罪魁祸首了,原来是我在

用memcpy函数时,第三个参数,即要拷贝的内存长度设错了,超过了应该设的长度,所以就出现了这一系列不可预知的错误。太

粗心大意了,千里之堤,毁于蚁穴,一句错误的代码,就可以让十几万行的程序崩溃,谨记!

 

这里记一下debugging tools 用法

以下zz from codeproject 


Try enabling the page heap[^] for your process. Follow these steps:
1. Download and install WinDBG[^].
2. Select “Start”->“All Programs”->“Debugging Tools for Windows”->“Global Flags”.
3. Select the “Image File” tab.
4. In the “Image: (TAB to refresh)” edit control enter the name of your app then press TAB. Just the name with the extension; not the full path.
5. Tick the following:
- “Enable page heap”
- “Enable heap tail checking”
- “Enable heap free checking”
- “Enable heap parameter checking”
- “Enable heap validation on call”
- “Create user mode stack trace database”
6. Press “Apply”.
7. Debug your application. Any debugger will do but with WinDBG you have access to the stack traces of allocations via the !heap –p –a command, for example. When a heap problem is detected a breakpoint will be generated.
8. When done un-tick all the options you ticked, press “Apply” then dismiss GFlags. This step is important as if it’s skipped all applications named as entered in step 4 will run with the page heap enabled.

Note that when using the page heap your application will run much slower than normal and consume way more memory. It’s good to have a beefy machine to do such tests; and such tests should be ran regularly on all applications you develop as part of regular testing activities. If I find a part of my application that’s too slow with the page heap enabled I optimize the memory allocation in that region. 

 

posted @ 2010-06-17 19:34  icecryed  阅读(866)  评论(0)    收藏  举报