Valgrind几乎对所有内容都给出了错误(警告:客户端切换堆栈?)

Valgrind gives an error for nearly everything (Warning: client switching stacks?)

本文关键字:警告 错误 客户端 堆栈 Valgrind      更新时间:2023-10-16

我正在以某种方式破坏内存,因为我的程序在随机位置崩溃而没有出错。

我将valgrind与--leak-check=full一起使用,与-O0 -g一起编译,它检测到的第一个问题是int main() 中的第一行

cout << "reading file" << endl;

带有

==5089== Warning: client switching stacks?  SP change: 0x7ff0004f8 --> 0x7feb7de10
==5089==          to suppress, use: --max-stackframe=4728552 or greater
==5089== Invalid write of size 8
==5089==    at 0x41E107: main (Dgn.cpp:2833)
==5089==  Address 0x7feb7de08 is on thread 1's stack

继续

==5089== Invalid read of size 8
==5089==    at 0x5DE6E10: std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.18)
==5089==    by 0x67AEDE4: (below main) (libc-start.c:260)
==5089==  Address 0x7feb7de08 is on thread 1's stack
==5089== 
==5089== Invalid write of size 8
==5089==    at 0x5DBF8F2: std::ios_base::ios_base() (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.18)
==5089==    by 0x5E06BFF: std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(char const*, std::_Ios_Openmode) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.18)
==5089==    by 0x41E131: main (Dgn.cpp:2834)
==5089==  Address 0x7feb7e1e8 is on thread 1's stack

指向

ifstream config_file("file");

几乎每一行都有错误。

是什么原因造成的?

我想我的第一个堆栈失败了!

从这里

随后是许多错误消息,如"无效读/写",其中包含一条注释:"地址在线程1的堆栈上",原因很简单。你只是在堆栈上分配了太大的变量——在我的例子中,我在一个函数中有太大的数组,作为局部变量。

缩小尺寸解决了这个问题。

为了指出显而易见的问题,您还可以按照valgrind的建议,即使用--max-stackframe=4728552更改最大堆栈帧。您直接解决了问题,但这也会抑制那些"无效读取"错误。

在Linux上,我对一个程序进行了评估,非常确信它没有超出堆栈。为了抑制这里显示的client switching stacks?错误,我使用了:

ulimit -s unlimited

现在valgrind按要求运行!