C++ glibc 检测 smallbin 链表损坏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19534051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:56:18  来源:igfitidea点击:

glibc detect smallbin linked list corrupted

c++memory-managementmemory-leaks

提问by user1583647

I am trying to run a function repeatedly in a large loop but I get an error after 2 or 3 iterations but if I start from the point it gave an error it works fine but again stops after 3 4 iteration. It might be a memory problem. As the function is quite large I am not sure where exactly there is a memory leakage. Is there anyway I can free the variables after each iteration or something that can solve this problem. Or as it is a linked list memory problem free all the linked list or something?What can be the solution? The problrm does not happen if I run the function once so I think it is because I am calling the function repeatedly in a loop. Is there any way to solve this problem?

我试图在一个大循环中重复运行一个函数,但在 2 或 3 次迭代后出现错误,但如果我从该点开始,它给出了一个错误,它工作正常,但在 3 4 次迭代后再次停止。可能是内存问题。由于函数非常大,我不确定内存泄漏的确切位置。无论如何我可以在每次迭代后释放变量或可以解决这个问题的东西。或者因为它是一个链表内存问题,释放所有链表什么的?有什么解决办法?如果我运行该函数一次,则不会发生问题,所以我认为这是因为我在循环中重复调用该函数。有没有办法解决这个问题?

The error is

错误是

**glibc detected:.....malloc():smallbin double linked list corrupted: 0x000000000 1d404c0 ***

回答by Jan Hudec

The library is telling you that the memory metadata is corrupt. That won't happen by mere memory leak, you had to write to invalid pointer. Either you wrote to index out of bounds or you wrote to pointer after it was freed.

该库告诉您内存元数据已损坏。这不会仅仅因为内存泄漏而发生,您必须写入无效指针。要么您写入越界索引,要么在指针被释放后写入指针。

The easiest way to debug this kind of issue is using valgrind. It only works under Linux, but you seem to be using that already. It is rather slow, because it single-steps the program and checks every memory-accessing instruction, but it can catch invalid memory access and also use of uninitialized variables and memory leaks very reliably.

调试此类问题的最简单方法是使用valgrind。它仅适用于 Linux,但您似乎已经在使用它了。它相当慢,因为它单步执行程序并检查每个内存访问指令,但它可以非常可靠地捕获无效内存访问以及未初始化变量和内存泄漏的使用。

There is also duma (detect unintended memory access)library. It can also be made to work on other platforms and is a bit faster, but it uses much more memory.

还有duma(检测意外内存访问)库。它也可以在其他平台上工作,速度稍快一些,但它使用更多的内存。

And there is gcc's own mudflapthat can be activated by specific compiler options. That one should work on most gcc targets, but I am not sure how complete the C++ support is.

并且有 gcc 自己的mudflap可以通过特定的编译器选项激活。那个应该适用于大多数 gcc 目标,但我不确定 C++ 支持有多完整。

Update (11/2018): mudflap is mostly superseded by Google Sanitizers, which are part of Clang.

更新 (11/2018):mudflap 大部分被Google Sanitizers取代,后者是Clang 的一部分。

回答by Ayberk ?zgür

Warning: Very specific use case

警告:非常具体的用例

I had this problem within an application written with Qt where I was using pointers to an object derived from QObjectthat had a copy constructor. I somehow overlooked the following warning during compilation:

我在一个用 Qt 编写的应用程序中遇到了这个问题,我在那里使用指向一个对象的指针,该对象派生自QObject具有复制构造函数的对象。我在编译过程中以某种方式忽略了以下警告:

warning: base class ‘class QObject' should be explicitly initialized in the copy constructor [-Wextra]

It turns out that QObject-derived objects are not supposed to be copied, but I did not expect the copy constructor on my QObject-derived class to cause this kind of error.

事实证明,不应该复制-QObject派生对象,但我没想到我的-派生类上的复制构造函数会导致这种错误。QObject