问题
Me and my team are having an issue with Visual Studio displaying gibberish instead of proper string contents when I pause my program. The string inside has correct data, it's just that the debugger of VS gets lost somehow.
I marked the correct contents in green, incorrect in red.
You can see that the std::string
defined as
const std::string testStdString = "contents of std::string";
Displays as "\bÄĎD\x19"
in debug hover and watch window. But the C-string from .c_str()
displays fine. Console written to with cout
also displays fine.
This broken value changes on each run.
In raw view mode, you can see that the pointer contents, when displayed as ASCII, look like what the debugger thinks are the string contents. So maybe some short string optimization related issue?
If you decode that as a string:
"\bÄĎD\x19"
[0x00000000]: 0x08 '\b'
[0x00000001]: 0xc4 'Ä'
[0x00000002]: 0xcf 'Ď'
[0x00000003]: 0x44 'D'
[0x00000004]: 0x19 '\x19'
[0x00000005]: 0x00 '\0'
It matches the buf
property visible in the raw view: 0x0000001944cfc408
(reverse order).
Issue only affects std::string
, std::wstring
and C-strings work fine. When I try to use std::string::c_str()
it works fine too.
When I try with a simple new project, this issue doesn't occur - but on our 18 solution with native C++ and C# UWP solution it does.
We don't use any non-Microsoft compiler nor standard library. We use the VC142 compiler with /permissive-
(conformance mode) all on the most up to date Visual Studio 2019 (16.3.4). Tried with VS141, doesn't help.
This started to happen after some update and was not fixed at least until VS 16.3.4. The previous version it worked on for sure was VS 2017, but some of my team say perhaps the early 2019 versions worked for them too.
Possibly related (but unanswered and about VS 2013): Visual Studio 2013 debugger showing weird values for std::string
I reported the issue in the Visual Studio Developer Community, maybe they'll know something.
Update 2019-10-22:
Tried to create a minimal project by copying project and removing libraries, referenced projects and shared projects but keeping the configuration of the main project and solution - can't replicate the issue this way. So it's not (just) the configuration, it's something about the linked projects and libraries. The projects/libs are either built by VS, or are from Windows SDK or Intel Media SDK.
Will try to remove them one by one later on, maybe this will help pinpoint the issue.
回答1:
Someone on my team found a potential fix, we did have different CRTs. One of the libraries was /MD
while the others were /MDd
. But changing that, did not fix much.
We used /MD
, because under /MDd
the library was malfunctioning. The library in question was the Intel Media SDK's mfx_dispatch project.
What fixed this was changing the toolset of that library from 141 to 142. On the latter, the std::string
was readable in the watch window.
Still having different problems with debugger, but this was the worst one.
来源:https://stackoverflow.com/questions/58410767/visual-studio-debugger-doesnt-display-stdstring-properly-in-debug