I'm building a game in UE5 with C . 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
When I look at the Visual Studio debugger it shows the assertion error:
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 of2
, not 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
).
If I click into the frame where it's validating the array index, the Locals window does show Index
is 65533
.
See a screenshot of this issue here: Visual Studio debugger
Per this screenshot the variable Handle
was optimized away, but it seems it was optimized to have the wrong value. I can't imagine this is a bug in the C compiler, so what else could it be?
CodePudding user response:
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
.
In case others run into this, it's not enough to use the "DebugGame Editor" config in the Visual Studio project for your game. 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. 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.