C++ 是否有类似 Valgrind Memcheck 的 Windows 工具,可以在免费错误后调试使用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9911889/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there Valgrind Memcheck like tool for windows to debug use after free errors?
提问by ks1322
Durring my work I regulary face rather common programming error - using some object which has already been freed. This invokes UB in C++. On linux, this kind of problems are usually resolved by using Valgrind tool Memcheck. From Memcheck manual:
在我的工作期间,我经常面临相当常见的编程错误 - 使用一些已经被释放的对象。这将调用 C++ 中的 UB。在linux 上,这类问题通常通过使用 Valgrind 工具 Memcheck 来解决。从Memcheck 手册:
Memcheck tries to establish what the illegal address might relate to, since that's often useful. So, if it points into a block of memory which has already been freed, you'll be informed of this, and also where the block was freed.
Memcheck 尝试确定非法地址可能与什么相关,因为这通常很有用。所以,如果它指向一个已经被释放的内存块,你会被告知这一点,以及该块被释放的位置。
Memcheck provides me call stack, where the object was deallocated and I can go on and debug the problem. Is there similar tool for windowswith the same functionality, preferably free?
Memcheck 为我提供了调用堆栈,其中对象被释放,我可以继续调试问题。是否有具有相同功能的Windows 的类似工具,最好是免费的?
采纳答案by ks1322
According to Dr. Memory documentation, there is -delay_frees_stack
option with exactly the same Valgrind functionality. From Option Reference:
根据 Dr. Memory 文档,存在-delay_frees_stack
具有完全相同 Valgrind 功能的选项。从选项参考:
-delay_frees_stack
default: false
Record callstacks on free to use when reporting use-after-free or other errors that overlap with freed objects. There is a slight performance hit incurred by this feature for malloc-intensive applications.
Also here is an example of error reported by Dr. Memory:
这里还有一个由 Dr. Memory 报告的错误示例:
Here is another example, using the -delay_frees_stack option to obtain the callstack of the freed memory:
Error #8: UNADDRESSABLE ACCESS: reading 0x001338a8-0x001338ac 4 byte(s)
# 0 unaddr_test1 [e:\derek\drmemory\git\src\tests\suppress.c:110]
# 1 test [e:\derek\drmemory\git\src\tests\suppress.c:269]
# 2 main [e:\derek\drmemory\git\src\tests\suppress.c:297]
Note: @0:00:02.141 in thread 3024
Note: next higher malloc: 0x001338e8-0x00133938
Note: prev lower malloc: 0x001337e8-0x00133820
Note: 0x001338a8-0x001338ac overlaps memory 0x001338a8-0x001338c4 that was freed here:
Note: # 0 test [e:\derek\drmemory\git\src\tests\suppress.c:269]
Note: # 1 main [e:\derek\drmemory\git\src\tests\suppress.c:297]
Note: instruction: mov (%eax) -> %eax
回答by stanwise
As Lailin Chen pointed out in his answer to thisquestion try one of these:
正如 Lailin Chen 在他对这个问题的回答中所指出的,请尝试以下方法之一:
Dr. Memory: https://github.com/dynamorio/drmemory
记忆博士:https: //github.com/dynamorio/drmemory
UMDH: http://support.microsoft.com/kb/268343
UMDH:http: //support.microsoft.com/kb/268343
AppVerifier: http://msdn.microsoft.com/en-us/library/dd371695%28v=vs.85%29.aspx
AppVerifier:http: //msdn.microsoft.com/en-us/library/dd371695%28v=vs.85%29.aspx
回答by SigTerm
The method that worked for mewas to write custom memory manager that provides global operators "new" and "delete", and lock every freed/usued memory block with VirtualProtect. This way any attempt to use freed memory will immediately trigger access violation which you can catch and debug. However, to be able to do this, you must "grab" all available memory (or 3/4 of it) using something like VirtualAlloc
and every memory block you return (from this initially allocated block) must be PAGE_SIZE
aligned (see GetSystemInfo
documentation), otherwise you won't be able to lock it reliably. Which means that even trivial application might require large amount of memory to use this method.
对我有用的方法是编写自定义内存管理器,提供全局运算符“new”和“delete”,并使用 VirtualProtect 锁定每个释放/使用的内存块。这样,任何使用已释放内存的尝试都会立即触发您可以捕获和调试的访问冲突。但是,为了能够做到这一点,您必须使用类似的东西“抓取”所有可用内存(或其中的 3/4),VirtualAlloc
并且您返回的每个内存块(从这个最初分配的块中)必须PAGE_SIZE
对齐(参见GetSystemInfo
文档),否则你将无法可靠地锁定它。这意味着即使是微不足道的应用程序也可能需要大量内存才能使用此方法。
As for "valgrind alternative for windows" - I havent heard of it. Somebody somewhere posted that it might be possible to compile/use valgrind with cygwin, but I don't know if this is true or not.
至于“windows 的 valgrind 替代品” - 我还没有听说过。有人在某处张贴说可能可以用 cygwin 编译/使用 valgrind,但我不知道这是否属实。
回答by ks1322
Here's a valiant Valgring attempt, and I wish them the best:
这是一次勇敢的 Valgring 尝试,我祝他们一切顺利:
http://sourceforge.net/p/valgrind4win/wiki/Home/
http://sourceforge.net/p/valgrind4win/wiki/Home/
I am afraid, though, that in order to implement a proper "Valgrind for Windows", access to Windows source code is required.
不过,恐怕要实现适当的“Valgrind for Windows”,需要访问 Windows 源代码。
IOW: When pigs fly.
IOW:当猪飞的时候。
回答by Frank
What worked best for me was using Visual leak Detector, all I needed to do was to include:
最适合我的是使用Visual leak Detector,我需要做的就是包括:
#include <vld.h>
at the beginning of the executables I wanted to test. Then running a debug executable from within windows, would provide detailed information about all leaked memory. From the output you can directly get to the line where the memory was allocated, so you can take care
在我想测试的可执行文件的开头。然后从 Windows 中运行调试可执行文件,将提供有关所有泄漏内存的详细信息。从输出中您可以直接到达分配内存的行,因此您可以小心