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

user1382306 picture user1382306 · Apr 26, 2014 · Viewed 11.3k times · Source

I'm corrupting memory somehow because my program crashes without error at random places.

I'm using valgrind with --leak-check=full, compiling with -O0 -g, and the very first problem it detects is the first line in int main()

cout << "reading file" << endl;

with

==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

It goes on with

==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

which points to

ifstream config_file("file");

Nearly every line has an error.

What causes this?

Answer

user1382306 picture user1382306 · Apr 26, 2014

I think I blew my first stack!

From here

Followed by many error messages like "Invalid read/write" containing a note: "Address is on thread 1's stack" then the cause is very simple. You are just allocating too large variables on stack - in my case I had too large array, as local variable, in one of functions.

Reducing sizes fixed the problem.