ks1*_*322 23 c++ windows debugging valgrind
在我的工作中,我经常面对相当常见的编程错误 - 使用一些已经被释放的对象.这在C++中调用UB.在linux上,通常使用Valgrind工具Memcheck来解决这类问题.来自Memcheck手册:
Memcheck试图确定非法地址可能涉及的内容,因为这通常很有用.因此,如果它指向已经释放的内存块,您将被告知这一点,以及块被释放的位置.
Memcheck为我提供了调用堆栈,对象被解除分配,我可以继续调试问题.是否有类似的工具用于具有相同功能的Windows,最好是免费的?
sta*_*ise 17
正如Lailin Chen在回答这个问题时指出的那样尝试其中一个:
记忆博士:https://github.com/dynamorio/drmemory
UMDH:http://support.microsoft.com/kb/268343
AppVerifier:http://msdn.microsoft.com/en-us/library/dd371695%28v=vs.85%29.aspx
对我有用的方法是编写自定义内存管理器,它提供全局操作符"new"和"delete",并使用VirtualProtect锁定每个释放/用过的内存块.这样,任何使用释放内存的尝试都会立即触发您可以捕获和调试的访问冲突.但是,为了能够执行此操作,您必须使用类似的东西"抓取"所有可用内存(或其中的3/4),VirtualAlloc并且返回的每个内存块(来自此初始分配的块)必须PAGE_SIZE对齐(请参阅GetSystemInfo文档),否则你将无法可靠地锁定它.这意味着即使是简单的应用程序也可能需要大量内存才能使用此方法.
至于"valgrind替代窗户" - 我没听说过.有人在某处发布了可能用cygwin编译/使用valgrind,但我不知道这是否属实.
小智 5
这是一次勇敢的 Valgring 尝试,我祝他们一切顺利:
http://sourceforge.net/p/valgrind4win/wiki/Home/
不过,恐怕要实现适当的“Valgrind for Windows”,需要访问 Windows 源代码。
IOW:当猪飞的时候。
根据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.
Run Code Online (Sandbox Code Playgroud)
这也是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
Run Code Online (Sandbox Code Playgroud)