当程序删除时崩溃时如何调试?

发布于 2024-11-26 03:39:01 字数 1162 浏览 3 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

随心而道 2024-12-03 03:39:01

我假设您想知道为什么代码片段在您发布的链接中崩溃。问题出在这一行。

*(ptr - 1) = 'x'; // i.e. ptr[-1] = 'x';

执行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.

*(ptr - 1) = 'x'; // i.e. ptr[-1] = 'x';

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.

风尘浪孓 2024-12-03 03:39:01

当程序删除时崩溃时如何调试?

如果您使用的是 Linux 或平台上可用的其他内存调试器,请在 Valgrind 下运行它。

How to debug when program crashes on delete?

Run it under Valgrind if you are on linux or with some other Memory Debugger available on your platform.

淤浪 2024-12-03 03:39:01

调试调试构建、拥有源代码、安装 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 having new) may be the source of problem you are encountering. Check the code!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文