简体   繁体   中英

GDB prints wrong values

I've got a bit of a problem with debugging a C++ program using GDB.

When I use print object.member , it doesn't always print the value of the variable correctly. Instead, it prints the value of one of the arguments to the function I'm debugging. And it doesn't change through the function, although I change the value of object.member throughout.

And the thing is, the program is rather large and consists of several modules, with partially specialised templates and such, so I can't post it all here.
Now I tried to create a minimal testcase, but whatever simple I tried, I can't make it work. I mean, not work.

So all I can ask is, has anybody ever seen this behaviour in GDB, and have you found out what caused it and how to solve it?

There are question here about similar behaviour, but those amount to the program not being compiled properly (optimisation levels too high etc). I compiled it with -Wall -Wextra -pedantic -g -O0 , so that can't be it.

And the program runs fine; I can cout << object.member; and that outputs the expected value, so I don't know what to try now.

I've seen similar behaviour before. Unfortunately, gdb is really 'C' based so although it will deal with C++, I've found it occasionally to be quite picky about displaying values.

When displaying more complex items (such as maps, strings or the dereferenced contents of smart pointers) you have to sometimes be quite explicit about dereferencing and casting variables.

Another possibility is the function itself - anything unusual about it? Is it templated for example?

Can you create a reference to this variable in your code and try displaying that? Or take the address of the variable and derefrence the contents - only if it's publicly available of course.

Naturally the source code must match what you've compiled so must be older than the exe but gdb will normally warn you about such things

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM