简体   繁体   English

Visual Studio C++ 调试器如何显示与另一帧不一致的局部变量值?

[英]How can Visual Studio C++ debugger be showing values of local variables that are at odds with another frame?

I'm building a game in UE5 with C++.我正在使用 C++ 在 UE5 中构建游戏。 If you have access to the Unreal Engine source code (get it here ), I'm hitting an assertion on this line: https://github.com/EpicGames/UnrealEngine/blob/d9d435c9c280b99a6c679b517adedd3f4b02cfd7/Engine/Plugins/Runtime/StateTree/Source/StateTreeModule/Private/StateTreeExecutionContext.cpp#L682如果您可以访问虚幻引擎源代码(在此处获取),我将在此行上打一个断言: https://github.com/EpicGames/UnrealEngine/blob/d9d435c9c280b99a6c679b517adedd3f4b02cfd7/Engine/Plugins//Runtime/StateTree源/StateTreeModule/Private/StateTreeExecutionContext.cpp#L682

When I look at the Visual Studio debugger it shows the assertion error:当我查看 Visual Studio 调试器时,它显示断言错误:

Array index out of bounds: 65533 from an array of size 5

But when I look at the Locals window that array index (stored in CurrentStatus.State.Index ) has a value of 2 , not 65533 .但是当我查看 Locals window 时,该数组索引(存储在CurrentStatus.State.Index中)的值为2 ,而不是65533 How can this be?怎么会这样?

The relevant source code is:相关源代码为:

    for (FStateTreeHandle Handle = CurrentStatus.State; Handle.IsValid(); Handle = StateTree->States[Handle.Index].Parent)
    {
        const FBakedStateTreeState& State = StateTree->States[Handle.Index];
        ...
    }

The assertion is hit the first time through the for loop when calling StateTree->States[Handle.Index] , so Handle.Index is getting the value CurrentStatus.State.Index (which is 2 ).在调用StateTree->States[Handle.Index]时,断言第一次通过 for 循环命中,因此Handle.Index获得的值CurrentStatus.State.Index (即2 )。

If I click into the frame where it's validating the array index, the Locals window does show Index is 65533 .如果我单击它正在验证数组索引的框架,则 Locals window 确实显示Index is 65533

See a screenshot of this issue here: Visual Studio debugger在此处查看此问题的屏幕截图: Visual Studio 调试器

Per this screenshot the variable Handle was optimized away, but it seems it was optimized to have the wrong value.根据此屏幕截图,变量Handle已被优化掉,但似乎已优化为具有错误的值。 I can't imagine this is a bug in the C++ compiler, so what else could it be?我无法想象这是 C++ 编译器中的错误,那还能是什么?

Turns out the comments on the question gave me the right clue here:原来对这个问题的评论给了我正确的线索:

The bottom line is that you cannot simply debug optimized code, and expect the debugger to adjust itself to the optimizations done by the compiler.最重要的是,您不能简单地调试优化代码,并期望调试器根据编译器完成的优化进行自我调整。

When I debugged using a non-optimized build of UE5, I quickly saw the issue, and the CurrentStatus.State.Index is in fact 65533 .当我使用 UE5 的非优化版本进行调试时,我很快发现了问题,并且CurrentStatus.State.Index实际上是65533

In case others run into this, it's not enough to use the "DebugGame Editor" config in the Visual Studio project for your game.如果其他人遇到这种情况,仅在 Visual Studio 项目中为您的游戏使用“DebugGame Editor”配置是不够的。 That config only compiles your game's code without optimizations, the engine is still run using optimized code.该配置仅编译您的游戏代码而不进行优化,引擎仍使用优化代码运行。

To run the engine without optimized code, you need to build it from source and use the "Debug Editor" Visual Studio config to disable optimizations.要在没有优化代码的情况下运行引擎,您需要从源代码构建它并使用“调试编辑器”Visual Studio 配置来禁用优化。 Then you can run your game by changing the path of the UE exe it uses in the Visual Studio project of your game from the project Property Pages under the Debugging section.然后,您可以通过在调试部分下的项目属性页中更改它在游戏的 Visual Studio 项目中使用的 UE exe 的路径来运行游戏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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