【问题标题】:*** glibc detected *** ./a.out: double free or corruption (top): 0x08901d70 *** while attempting to free a BST*** 检测到 glibc *** ./a.out:双重释放或损坏(顶部):0x08901d70 *** 尝试释放 BST 时
【发布时间】:2011-10-24 13:07:38
【问题描述】:

我确实意识到有一些“检测到 glibc”的帖子,但如果您能为此提出解决方案,我将不胜感激:

*** glibc detected *** ./a.out: double free or corruption (top): 0x08901d70 ***
======= Backtrace: =========
/lib/libc.so.6(+0x6c501)[0x17c501]
/lib/libc.so.6(+0x6dd70)[0x17dd70]
/lib/libc.so.6(cfree+0x6d)[0x180e5d]
/lib/libc.so.6(fclose+0x14a)[0x16c81a]
./a.out[0x8048998]
/lib/libpthread.so.0(+0x5cc9)[0xc1fcc9]
/lib/libc.so.6(clone+0x5e)[0x1e069e]
======= Memory map: ========

这似乎发生在我尝试释放二叉搜索树时:

void freetree(BNODEPTR *root)
{
        if(root!=NULL)
        {
                freetree(root->left);
                freetree(root->right);
                free(root);
        }
}  

结构的类型定义为 BNODEPTR

struct bnode{
        int info;
        int count;
        struct bnode* left;
        struct bnode* right;
};

我正在使用 freetree(root) 从 main() 调用函数。

这棵树似乎被正确地实现了,因为中序遍历产生了一个排序的输出。

整个代码在:

http://pastebin.com/Eieu3xDa

http://pastebin.com/jtGN6XKj

【问题讨论】:

    标签: c data-structures tree binary-search-tree


    【解决方案1】:

    我可以筛选你的源代码,但正如他们所说,“喂人一条鱼……”

    1. 使用调试符号编译您的代码(将-g 传递给编译器)。如果你这样做,你可以在回溯中得到一个函数名而不是./a.out[0x8048998]

    2. 使用 Valgrind 的 memcheck 工具(默认工具)运行您的代码。这可能会让您更好地了解错误在哪里。对于初学者,您只需安装 Valgrind 并运行 valgrind ./a.out

    特别是,我认为整个二叉树是一条红鲱鱼。您的程序在其他地方还有另一个问题。从回溯中,我可以看到(1)freetree 中没有触发错误消息;(2)您使用的线程很容易被滥用。

    【讨论】:

    • 使用 -g 选项编译后,我得到输出:pastebin.com/pzXTS9Qf。 valgrind ./a.out 的输出是pastebin.com/nZYJLmUF。我不确定我看到了实际的函数名称:(
    • @Chhama:看起来对我来说没有错误。你确定你在运行./a.out时出错了吗?
    • gdb 中运行程序并在malloc_error_break 上设置断点。看起来fclose 可能在同一个文件上被多次调用。
    猜你喜欢
    • 2014-04-29
    • 2012-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多