如何诊断 Windows 上的堆损坏错误? [英] How do I diagnose heap corruption errors on Windows?

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

问题描述

我使用 Windows 8.1 64 位和 Visual Studio 2013 Ultimate.我正在将一个程序从 Linux 移植到使用 C++、OpenGL 和 SDL 的 Windows.我在 Windows 上的 64 位上通过 cmake 自定义了适当的库.当我从 Visual Studio 运行程序时,IDE 说有一个磁头损坏.这并不奇怪,因为我使用指针来实例化对象,并且我正在使用原始指针,为了论证,我计划将其更改为智能指针.稍后我会做增强魔法.

I'm using Windows 8.1 64-bit with Visual Studio 2013 Ultimate. I am porting a program from Linux to Windows which uses C++, OpenGL and SDL. I have the appropriate libraries custom compiled through cmake on 64-bit on Windows. When I run the program from Visual Studio, the IDE says there's a head corruption. This is no surprise since I'm using pointers to instantiate objects, and I'm using raw pointers which I do plan to change to smart pointers for the sake of the argument. I'll be doing the boost magic later.

同时,我使用我的 Linux 计算机通过 Valgrind 诊断任何内存泄漏,并且 Valgrind 没有报告任何严重的问题.然后我继续使用 CppCheck 但那里也没有什么严重的.也许我在这里太宽容了,Windows 实际上可能比 Linux 更严肃地对待不太严肃的事情,这是一个惊喜,因为 MSVC 往往比 GCC 更宽容.

In the meantime, I used my Linux computer to diagnose any memory leaks through Valgrind and there was nothing serious reported from Valgrind. I then proceeded to use CppCheck but there was nothing serious on there either. Maybe I'm being too lenient here and Windows might actually be taking the less serious stuff more serious than Linux does, which is a surprise since MSVC tends to be more forgiving than GCC.

因此,该程序适用于 Linux,不适用于 Windows.(太棒了!)而且 Visual Studio 没有帮助到处抛出异常,这让我更加讨厌 Windows.我开始在谷歌上搜索解决方案并遇到了一个叫做 gflags 或页面助手的东西,所以我安装了调试工具并尝试启动 gflags 但我不知道如何使用它!后来我发现你必须使用其他一些名为 adp 的工具,然后将 gflags 附加到它,所以当我启动 adp 时它崩溃了.所以现在我不知道该怎么做,并且正处于中止移植的边缘(这很有趣,因为许多人抱怨将程序从 Windows 移植到 Linux 有多么困难,而事实恰恰相反).

So, the program works on Linux and it doesn't on Windows. (Just great!) And Visual Studio isn't helping by throwing exceptions all over the place which makes me hate Windows even more. I started googling around for a solution and came across this thing called gflags or page helper, so I installed the debugging tools and tried to launch gflags but I have no idea how to use it! I then later found that you had to use some other tool called adp and then attach gflags to it, so when I launched adp it crashes. So now I have no idea what to do and am on the verge of aborting the port (which is funny since many people are complaining how hard it is to port programs from Windows to Linux while the opposite is true).

所以,现在我向这个社区寻求帮助:如何调试/诊断在 Windows 上而不是在 Linux 上发生的堆损坏错误?我真的应该使用 gflags 还是应该用我的胆量?

So, now I appeal to this community for help: how do I debug/diagnose heap corruption errors that occur on Windows but not on Linux? Am I really supposed to be using gflags or should I just use my guts on this?

推荐答案

使用调试堆并在 main() 的最开始调用它.

Use the debug heap and call this at the very beginning in main().

_CrtSetDbgFlag(_CRTDBG_CHECK_ALWAYS_DF);

它会大大减慢程序的速度,但一旦发生损坏,它就会立即中断.

It will slow down the program a lot but it should break as soon as corruption occurs.

详情请参考这篇文章:https://msdn.microsoft.com/en-us/library/974tc9t1.aspx#BKMK_Check_for_heap_integrity_and_memory_leaks

这篇关于如何诊断 Windows 上的堆损坏错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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