Win32下的堆损坏;如何定位? [英] Heap corruption under Win32; how to locate?

查看:141
本文介绍了Win32下的堆损坏;如何定位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理破坏堆的多线程 C ++应用程序。找到这种腐败的常用工具似乎不适用。旧的生成(18个月)的源代码展示与最新版本相同的行为,所以这已经存在了很长时间,只是没有注意到;在下面,源三角洲不能用于识别何时引入了错误 - 在存储库中有很多的代码更改。

I'm working on a multithreaded C++ application that is corrupting the heap. The usual tools to locate this corruption seem to be inapplicable. Old builds (18 months old) of the source code exhibit the same behaviour as the most recent release, so this has been around for a long time and just wasn't noticed; on the downside, source deltas can't be used to identify when the bug was introduced - there are a lot of code changes in the repository.

崩溃behaviuor的提示是在这个系统中生成吞吐量 - 数据的套接字传输,将其转换为内部表示。我有一组测试数据,会定期导致应用程序异常(各种地方,各种原因 - 包括堆分配失败,因此:堆损坏)。

The prompt for crashing behaviuor is to generate throughput in this system - socket transfer of data which is munged into an internal representation. I have a set of test data that will periodically cause the app to exception (various places, various causes - including heap alloc failing, thus: heap corruption).

似乎与CPU功耗或内存带宽有关;每台机器越多,就越容易崩溃。禁用超线程核心或双核心核心会降低(但不会消除)损坏的速率。

The behaviour seems related to CPU power or memory bandwidth; the more of each the machine has, the easier it is to crash. Disabling a hyper-threading core or a dual-core core reduces the rate of (but does not eliminate) corruption. This suggests a timing related issue.

现在这里是rub:

当它在轻量级调试环境下运行时(比如说 Visual Studio 98 / AKA MSVC6 )堆溢出是相当容易复制 - 十分钟或十五分钟之前,一些失败的可怕和异常,如 alloc; 当在复杂的调试环境(Rational Purify, VS2008 / MSVC9 或甚至Microsoft应用程序验证器)下运行时,系统将成为内存速度限制, bound:CPU没有超过 50%,磁盘灯不亮,程序运行得很快,盒子消耗 1.3G 2G的RAM)。因此,我可以选择能够重现问题(但不能识别原因),或者能够确定原因或无法重现的问题。

Now here's the rub:
When it's run under a lightweight debug environment (say Visual Studio 98 / AKA MSVC6) the heap corruption is reasonably easy to reproduce - ten or fifteen minutes pass before something fails horrendously and exceptions, like an alloc; when running under a sophisticated debug environment (Rational Purify, VS2008/MSVC9 or even Microsoft Application Verifier) the system becomes memory-speed bound and doesn't crash (Memory-bound: CPU is not getting above 50%, disk light is not on, the program's going as fast it can, box consuming 1.3G of 2G of RAM). So, I've got a choice between being able to reproduce the problem (but not identify the cause) or being able to idenify the cause or a problem I can't reproduce.

我目前最好的猜测是下一步是什么:

My current best guesses as to where to next is:


  1. 得到一个疯狂的替换当前开发盒:2Gb RAM在 E6550 Core2 Duo );这将有可能重现崩溃导致在强大的调试环境下运行时的错误行为;或

  2. 重写操作符 new delete 以使用 VirtualAlloc VirtualProtect ,以便在完成后将内存标记为只读。在 MSVC6 下运行,让操作系统捕获写入释放内存的坏人。是的,这是一个绝望的迹象:谁是地狱重写删除?我不知道这是否会像Purify等人一样慢。

  1. Get an insanely grunty box (to replace the current dev box: 2Gb RAM in an E6550 Core2 Duo); this will make it possible to repro the crash causing mis-behaviour when running under a powerful debug environment; or
  2. Rewrite operators new and delete to use VirtualAlloc and VirtualProtect to mark memory as read-only as soon as it's done with. Run under MSVC6 and have the OS catch the bad-guy who's writing to freed memory. Yes, this is a sign of desperation: who the hell rewrites new and delete?! I wonder if this is going to make it as slow as under Purify et al.

和,no:不是一个选项。

And, no: Shipping with Purify instrumentation built in is not an option.

一个同事刚刚走过去,问Stack Overflow?现在我们是否堆栈溢出?

A colleague just walked past and asked "Stack Overflow? Are we getting stack overflows now?!?"

现在,问题:如何找到堆腐败器?

更新:平衡 new [] delete [] 似乎已经有很长的路要解决问题。而不是15分钟,应用程序现在大约两个小时前崩溃。还没有。任何进一步的建议?堆错误仍然存​​在。

Update: balancing new[] and delete[] seems to have gotten a long way towards solving the problem. Instead of 15mins, the app now goes about two hours before crashing. Not there yet. Any further suggestions? The heap corruption persists.

更新:Visual Studio 2008下的发行版本似乎更好;当前的怀疑依赖于 VS98 附带的 STL 实施。

Update: a release build under Visual Studio 2008 seems dramatically better; current suspicion rests on the STL implementation that ships with VS98.



  1. 重现问题。 Dr Watson 将产生一个可能有助于进一步分析的转储。

  1. Reproduce the problem. Dr Watson will produce a dump that might be helpful in further analysis.


我会记下这一点,但我担心Watson博士只会

I'll take a note of that, but I'm concerned that Dr Watson will only be tripped up after the fact, not when the heap is getting stomped on.


另一个尝试可能是使用 WinDebug

Another try might be using WinDebug as a debugging tool which is quite powerful being at the same time also lightweight.

这是一个非常强大的调试工具,时刻,再次:没有多大帮助,直到出现问题。

Got that going at the moment, again: not much help until something goes wrong. I want to catch the vandal in the act.


也许这些工具将允许你至少将问题缩小到某个组件。

Maybe these tools will allow you at least to narrow the problem to certain component.

我不抱希望,但绝望的时间要求...

I don't hold much hope, but desperate times call for...

您确定项目的所有组件都有正确的运行时库设置( C / C ++选项卡,VS 6.0中的代码生成类别项目设置)?

And are you sure that all the components of the project have correct runtime library settings (C/C++ tab, Code Generation category in VS 6.0 project settings)?

不,我不会,明天我会花几个小时通过工作区在它)和检查他们都编译和链接与适当的标志。



更新:这需要30秒。在设置对话框中选择所有项目,取消选择,直到找到没有正确设置的项目(它们都有正确的设置)。

No I'm not, and I'll spend a couple of hours tomorrow going through the workspace (58 projects in it) and checking they're all compiling and linking with the appropriate flags.


Update: This took 30 seconds. Select all projects in the Settings dialog, unselect until you find the project(s) that don't have the right settings (they all had the right settings).

推荐答案

我的首选是一个专用的堆工具,例如 pageheap.exe

My first choice would be a dedicated heap tool such as pageheap.exe.

重写新的和删除可能是有用的,由低级代码提交的分配。如果这是你想要的,最好使用Microsoft Detours Detour 低级别的alloc API

Rewriting new and delete might be useful, but that doesn't catch the allocs committed by lower-level code. If this is what you want, better to Detour the low-level alloc APIs using Microsoft Detours.

例如:验证运行时库匹配(发布与调试,多线程vs.单线程,dll与静态库),查找不良删除(例如,delete where delete []) ),请确保您不混用和匹配您的资金。

Also sanity checks such as: verify your run-time libraries match (release vs. debug, multi-threaded vs. single-threaded, dll vs. static lib), look for bad deletes (eg, delete where delete [] should have been used), make sure you're not mixing and matching your allocs.

也请尝试选择性地关闭主题,看看什么时候/如果问题消失了。

Also try selectively turning off threads and see when/if the problem goes away.

在第一次异常时,调用堆栈是什么样子?

What does the call stack etc look like at the time of the first exception?

这篇关于Win32下的堆损坏;如何定位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆