Variable is different when turn on/off debug

Hello, very strange problem here. I have a piece of code that does some vector calculation, nothing special.
When I test the code with DrawDebugSphere on the result is correct and consistent.
However, if I comment out the debugs stuffs the result is completely different.
As soon as I use UE_LOG or DrawDebug to monitor the variables again the result back to correct.
What could be happening? Is this some garbage collection problem?

highly unlikely. How do you know the result is different in the case where you don’t log it? Do you just look at the debugger values or does your game actually behave differently? Maybe show the code in question.

Hello, I have fixed the problem but I seriously have no idea how or why is this different. I simply change the code from:

FRotator RootTemp = FRotator(0,0,0);              
FRotator RootMotionRotation = RootMotionTransform.GetRotation().Rotator();
Skeletal->TransformFromBoneSpace(Skeletal->GetBoneName(0),
			                              RootMotionTransform.GetTranslation()*RootMotionTranslationScale,
			                               RootMotionTransform.GetRotation().Rotator(),
			                               RootEndLocation,
			                               RootTemp);

To

FRotator RootTemp = FRotator(0,0,0);
FVector RootMotionTranslation=RootMotionTransform.GetTranslation()*RootMotionTranslationScale;
FRotator RootMotionRotation = RootMotionTransform.GetRotation().Rotator();
Skeletal->TransformFromBoneSpace(Skeletal->GetBoneName(0),
			                                 RootMotionTranslation,
			                                 RootMotionRotation,
			                                 RootEndLocation,
			                                 RootTemp);

The first piece of code ONLY WORKS if there is a UE_LOG, or a DrawDebugSphere logging any values.
The seconds work with or without log. The only thing I did was to make a local variable of function parameters first. Please tell me if you spot any problems, I don’t want to meet this again in the future.

Looking at your code, there shouldn’t be any difference between the two versions. The important thing here is, does your code actually behave differently with logging, i.e. do you observe different effects in your game (do you use RootEndLocation and RootTemp for something after the function call)? If you just look at it in the debugger and observe different values for RootEndLocation and RootTemp then there is likely no problem because the debugger sometimes can’t show the correct values due to compiler optimizations. But if you actually observe different behaviour in your game with and without logging there are 3 possibilities:

1.) You yourself have undefined behaviour in your code.
2.) It is a subtle engine bug, which is not completely unlikely, because GetTranslation() and GetRotation() do some heavy optimization trickery with unaligned memory under the hood.
3.) It is a compiler bug (least likely). I assume you use Visual Studio?

I am using Rider for this. I do observe the difference between having UE_LOG and not.
With the UE_LOG, the piece of code CONSISTENTLY calculate the right result and it CONSISTENTLY produces the wrong result without.

The code I have here is to simulate a root motion montage to calculate the root position at the end of simulation time. What happen when it works right is that the RootEndLocation shows the expected location at the end of montage. When it works incorrectly, RootEndLocation shows root position at GetActorLocation(). So right at position of player, likely there is no simulation at all.

I have set break and check the input many time but in both situation, the input is exactly the same but producing different output/RootEndPosition.