当程序删除时崩溃时如何调试?
当delete
执行时,程序崩溃。我尝试使用以下代码来检查损坏的堆块,但 if
条件结果为 false。
int rc;
if (_HEAPOK != (rc = _heapchk()))
{
switch(rc)
{
case _HEAPEMPTY:
puts("The heap has not been initialized.");
break;
case _HEAPBADNODE:
puts("A memory node is corrupted or the heap is damaged.");
break;
case _HEAPBADBEGIN:
puts("The heap specified is not valid.");
break;
}
}
完整的代码可以在这里找到: http://cyberkinetica.homeunix.net/os2tk45/xpg4ref/ 157_L2__heapchkValidateDefa.html
请让我知道如何调试这个案例。
编辑:
我尝试在文件中进行调试:
...\Microsoft Visual Studio 9.0\VC\crt\src\dbgheap.c
,但在以下功能中失败:
extern "C" _CRTIMP int __cdecl _CrtIsValidHeapPointer( const void * pUserData)
此函数的注释指出:
目的:验证指针不仅是一个有效的指针,而且来自 '本地'堆。来自 C 运行时的另一个副本(即使在同一进程中)的指针将被捕获。
这个本地堆是什么?它是否有助于找出问题?
When delete
executes the program crashes. I tried following code to check for corrupted heap block but if
condition results false.
int rc;
if (_HEAPOK != (rc = _heapchk()))
{
switch(rc)
{
case _HEAPEMPTY:
puts("The heap has not been initialized.");
break;
case _HEAPBADNODE:
puts("A memory node is corrupted or the heap is damaged.");
break;
case _HEAPBADBEGIN:
puts("The heap specified is not valid.");
break;
}
}
complete code can be found here: http://cyberkinetica.homeunix.net/os2tk45/xpg4ref/157_L2__heapchkValidateDefa.html
Please let me know how to debug in this case.
EDIT:
I tried to debug in file:
...\Microsoft Visual Studio 9.0\VC\crt\src\dbgheap.c
and it's failing in following function:
extern "C" _CRTIMP int __cdecl _CrtIsValidHeapPointer(
const void * pUserData)
Comments on this function states:
Purpose: Verify pointer is not only a valid pointer but also that it is from the 'local' heap. Pointers from another copy of the C runtime (even in the same process) will be caught.
what is this local heap and does it help in finding out the issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我假设您想知道为什么代码片段在您发布的链接中崩溃。问题出在这一行。
执行
ptr = malloc()
后,执行上面的命令。指针正在写入超出范围的内存区域;这会导致未定义的行为。幸好系统崩溃了。
I assume that you want to know why the code snippet is crashing in your posted link. The problem is in this line.
After doing
ptr = malloc()
, you execute above command.Pointer is writing in the memory area which is going out of range; which results in undefined behavior. Luckily the system crashes.
如果您使用的是 Linux 或平台上可用的其他内存调试器,请在 Valgrind 下运行它。
Run it under Valgrind if you are on linux or with some other Memory Debugger available on your platform.
调试调试构建、拥有源代码、安装 VS - 这是最简单的事情之一。在调试器中运行程序,看看它在哪里崩溃。检查调用堆栈并查看它通向何处。
对于您的问题,伪造的
delete
(没有new
)可能是您遇到的问题的根源。检查代码!Debugging a debug-build, having source code, having VS installed - this is one of the easiest thing. Run the program within the debugger, and see where it is crashing. Check the call-stack and see where it leads.
For your problem, A bogus
delete
(not havingnew
) may be the source of problem you are encountering. Check the code!