*** 检测到 glibc *** ./a.out:双重释放或损坏(顶部):0x08901d70 *** 尝试释放 BST 时
我确实意识到有一些“检测到 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);
}
}
该结构被 typedef'd 为 BNODEPTR
struct bnode{
int info;
int count;
struct bnode* left;
struct bnode* right;
};
我是使用 freetree(root) 从 main() 调用该函数。
该树似乎已正确实现,因为中序遍历会产生排序的输出。
整个代码位于:
I do realize there are some "glibc detected" posts but I would be very grateful if you could suggest a solution for this:
*** 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: ========
This seems to happen when I attempt to free a binary search tree:
void freetree(BNODEPTR *root)
{
if(root!=NULL)
{
freetree(root->left);
freetree(root->right);
free(root);
}
}
The structure is typedef'd to BNODEPTR
struct bnode{
int info;
int count;
struct bnode* left;
struct bnode* right;
};
I am calling the function from main() using freetree(root).
The tree seems to be implemented correctly as an inorder traversal produces a sorted output.
The entire code is at:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我可以筛选你的源代码,但正如他们所说,“喂人一条鱼......”
使用调试符号编译你的代码(将
-g
传递给编译器)。如果这样做,您可以在回溯中获得函数名称而不是./a.out[0x8048998]
。使用 Valgrind 的 memcheck 工具(默认工具)运行代码。这可能会让您更好地了解错误所在。对于初学者来说,您只需安装 Valgrind 并运行
valgrind ./a.out
。特别是,我认为整个二叉树是一个转移注意力的东西。您的程序在其他地方还有另一个问题。从回溯中,我可以看到(1)在
freetree
中没有触发错误消息,并且(2)您使用的是很容易被误用的线程。I could sift through your source code, but as they say, "Feed a man a fish..."
Compile your code with debugging symbols (pass
-g
to the compiler). If you do this, you can get a function name instead of./a.out[0x8048998]
in the backtrace.Run your code with Valgrind's memcheck tool (the default tool). This might give you a much better clue about where the error is. You can just install Valgrind and run
valgrind ./a.out
for starters.In particular, I think the whole binary tree is a red herring. There is another problem in your program somewhere else. From the backtrace, I can see that (1) the error message is not triggered in
freetree
and (2) you are using threads, which are easily misused.