本文介绍了如何调试堆腐败错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我在Visual Studio 2008下调试(本机)多线程C ++应用程序。在看似随机的场合,我得到一个Windows已触发了一个断点...错误,注意这可能是由于堆中的腐败。这些错误不会总是立即使应用程序崩溃,虽然很可能会在短时间后崩溃。 这些错误的最大的问题是,他们只弹出后 什么是多线程应用程序 如何调试这些错误? $ b 欢迎提示,工具,方法,启示...。解决方案 应用程序验证器与调试工具Windows 是一个了不起的设置。您可以将这两者同时作为 Windows驱动程序工具包或更轻的Windows SDK 的一部分。 (在研究早期问题关于堆损坏问题。)我曾经使用BoundsChecker和Insure ++(在其他答案中提到)过去,虽然我惊讶于应用程序验证程序中有多少功能。 p> 电篱笆(又名efence), dmalloc ,valgrind 等等都值得一提,但是大多数这些都比Windows更容易在* nix下运行。 Valgrind是非常灵活的:我已经调试了大型服务器软件与许多堆问题使用它。 当所有其他失败,你可以提供自己的全局运算符new / delete和malloc / calloc / realloc重载 - 如何做到这一点会有所不同,取决于编译器和平台 - 这将是一个投资 - 但它可以回报长期。理想的功能列表应该看起来很熟悉dmalloc和电炉,和令人惊讶的优秀书编写固体代码: sentry值:允许在每个alloc之前和之后多一点空间,填充幻数(帮助捕获缓冲区溢出和下溢,以及偶尔的野指针) alloc fill :使用魔法非0值 - Visual C ++已经在Debug版本中为您提供此功能(帮助捕获未初始化的变量的使用) 自由填充:使用魔法填充释放的内存非0值,如果在大多数情况下解除引用,则会触发segfault(帮助捕获悬挂指针) 延迟释放:不要将释放的内存堆有一段时间,保持自由填充但不可用(帮助捕获更多悬挂指针,捕获接近双重释放) 跟踪:能够记录 请注意,在我们的本地自制系统(对于嵌入式目标)中,我们保持跟踪单独从大多数其他的东西,因为运行时间的开销是高得多。 如果你有兴趣更多重载这些分配函数/运算符的原因,请查看我的答案任何原因重载全局运算符的新和删除?;无耻的自我宣传,它列出了其他技术,有助于跟踪堆腐败错误,以及其他适用的工具。 I am debugging a (native) multi-threaded C++ application under Visual Studio 2008. On seemingly random occasions, I get a "Windows has triggered a break point..." error with a note that this might be due to a corruption in the heap. These errors won't always crash the application right away, although it is likely to crash short after.The big problem with these errors is that they pop up only after the corruption has actually taken place, which makes them very hard to track and debug, especially on a multi-threaded application.What sort of things can cause these errors?How do I debug them?Tips, tools, methods, enlightments... are welcome. 解决方案 Application Verifier combined with Debugging Tools for Windows is an amazing setup. You can get both as a part of the Windows Driver Kit or the lighter Windows SDK. (Found out about Application Verifier when researching an earlier question about a heap corruption issue.) I've used BoundsChecker and Insure++ (mentioned in other answers) in the past too, although I was surprised how much functionality was in Application Verifier.Electric Fence (aka "efence"), dmalloc, valgrind, and so forth are all worth mentioning, but most of these are much easier to get running under *nix than Windows. Valgrind is ridiculously flexible: I've debugged large server software with many heap issues using it.When all else fails, you can provide your own global operator new/delete and malloc/calloc/realloc overloads -- how to do so will vary a bit depending on compiler and platform -- and this will be a bit of an investment -- but it may pay off over the long run. The desirable feature list should look familiar from dmalloc and electricfence, and the surprisingly excellent book Writing Solid Code:sentry values: allow a little more space before and after each alloc, respecting maximum alignment requirement; fill with magic numbers (helps catch buffer overflows and underflows, and the occasional "wild" pointer)alloc fill: fill new allocations with a magic non-0 value -- Visual C++ will already do this for you in Debug builds (helps catch use of uninitialized vars)free fill: fill in freed memory with a magic non-0 value, designed to trigger a segfault if it's dereferenced in most cases (helps catch dangling pointers)delayed free: don't return freed memory to the heap for a while, keep it free filled but not available (helps catch more dangling pointers, catches proximate double-frees)tracking: being able to record where an allocation was made can sometimes be usefulNote that in our local homebrew system (for an embedded target) we keep the tracking separate from most of the other stuff, because the run-time overhead is much higher.If you're interested in more reasons to overload these allocation functions/operators, take a look at my answer to "Any reason to overload global operator new and delete?"; shameless self-promotion aside, it lists other techniques that are helpful in tracking heap corruption errors, as well as other applicable tools. 这篇关于如何调试堆腐败错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!