Visual Studio:std::string 的调试手表出现乱码?
当我在 Visual Studio 2005 中调试 C++ 混合(托管/非托管)项目时,我经常从调试监视中得到奇怪的数据,如下所示:
(顺便说一句,变量 i_processName
是一个 const std::string &
)
alt text http://img175.imageshack.us/img175/3561/43419953av1.jpg
请注意,该变量实际上保存有效数据 - 如果我将其打印到标准输出,则打印字符串很好,谢谢您的询问。 更简单的数据类型(例如int)(通常?)会显示正确的值。
你也遇到过这种情况吗?
这是调试时的一个主要 PITA,所以...有关如何使手表显示正确数据的任何想法,或者导致此问题的原因是什么?
When I'm debugging C++ mixed (managed/unmanaged) projects in Visual Studio 2005, I often get weird data from the debug watches, like below :
(btw, the variable i_processName
is a const std::string &
)
alt text http://img175.imageshack.us/img175/3561/43419953av1.jpg
Note that the variable actually holds valid data - if i print it to stdout, the printed string is just fine, thanks for asking.
The simpler types of data (e.g. int
s) (usually?) get their correct values shown.
Did this ever happen to you too?
This is a major PITA when debugging, so ... any ideas on how to make the watches show the correct data, or what's causing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
自定义类型(包括 STL)的调试显示取决于位于
\Common7\Packages\Debugger
文件夹中的文件autoexp.dat
。 确保您的版本与您的库版本匹配,并且该文件的旧版本尚未保留(例如在升级时)。请注意,您还可以为其他类型编写自己的可视化工具,更多信息此处 和此处。 对于复杂的项目来说,这可以节省大量时间,非常值得在编写自定义可视化工具上付出(小的)努力。
Debug display of custom types (this includes the STL) depends on the file
autoexp.dat
located in the<install_path>\Common7\Packages\Debugger
folder. Make sure that yours matches your library version and that an older version of this file hasn't been kept around (when upgrading for example).Note that you can also write your own visualizers for other types, more info here and here. This is a major time saver for complex projects, well worth the (small) effort put into writing custom visualizers.
是的,我在调试器中看到了这个问题,
在我的例子中,它与 Unicode 与 NonUnicode 有关。
Yes, i see this problem in my debuger,
in my case its connected to Unicode vs NonUnicode.
看来您的调试符号不正确。
检查模块调试窗口(菜单:Debug>Windows)。 检查您正在调试的模块是否已“已加载符号”。 列在符号状态列下。 检查列出的符号文件是否是您认为应该是的文件。 您可以右键单击一个模块并获取有关 VS 如何加载符号的更多信息,您也可以重新加载它们。
如果您遇到符号加载问题,可以在“工具”>“选项”>“调试”>“符号”下设置路径和其他设置。
错误的调用堆栈也会导致此类问题。 确保堆栈中没有任何条目,例如“堆栈在这一点上可能不正确......”。 可以?
Visual Studio 在可视化工具中混淆本机数据类型和托管数据类型也可能很奇怪,但我对此表示怀疑。 屏幕截图中的弹出窗口看起来调试器知道变量是什么。
Looks like your debugging symbols are incorrect.
Check the modules debug window (menu: Debug>Windows). Check that modules you are debugging have "Symbols loaded." listed under the Symbol Status column. Check that the Symbol File listed is the file you think it should be. You can right click a module and get more info about how VS loaded the symbols and you can reload them as well.
If you're having symbol loading problems you can set paths and other settings under Tools>Options>Debugging>Symbols.
A bad call stack can cause issues like this as well. Make sure the stack doesn't have any entries like "the stack may be incorrect for this point...". Does it?
It also can be something odd with Visual Studio confusing native and manged data types in the visualizer, but I doubt it. The popup in your screen shot looks like the debugger know what the variable is.
一种想法是 std::string 的 STLPort 实现使用分割缓冲区实现。 它有一个小的静态缓冲区(我想说 14 个字符)和一个指向 char 数组的指针。 其中之一将无效,另一个将包含字符串数据,具体取决于存储的字符串的长度。 如果您使用 STLPort 或类似的实现,您的字符串可视化工具可能会查看错误的缓冲区,该缓冲区恰好包含垃圾数据。
One thought -- the STLPort implementation of std::string uses a split buffer implementation. It has a small static buffer (I want to say 14 characters) and a pointer to a char array. One of these will be invalid and the other will contain the string data, depending on the length of the stored string. If you're using STLPort or a similar implementation, your string visualizer might be looking at the wrong buffer, which happens to contain junk data.
我相信 Aardvark 可能找到了正确的答案。 如果我没记错的话,当您编译混合模式时,编译器会将尽可能多的 C++ 代码转换为在 CLR 上运行的代码,从而转换为 CLR 拥有的内存。 我的猜测是调试器对字符串的保存位置感到困惑 - 非托管内存还是托管内存。
I beleive that Aardvark is probably onto the correct answer. If i remember correctly when you are compiling mixed mode, the compiler will turn as much of the C++ code it can into code that runs on the CLR, and consequently memory that is owned by the CLR. My geuss is that the debugger is confused about where the string is held - unmanaged or managed memory.
这是 Visual Studio 调试器中一直存在的错误,并且在 Visual Studio 2019 中仍然出现。
它是随机的。
有时更改代码并重新编译可以解决问题,有时则不能。
This is a bug that has been in the Visual Studio debugger since forever and still occurs in Visual Studio 2019.
It is random.
Sometimes making a code change and recompiling resolves the problem, and sometimes it doesn't.