Debugging Optimized Code


Debugging optimized code

So, your code pointer is jumping around and you can’t see local variables in the debugger. You’re debugging optimized code.

You have a options, such as full debug builds, project settings, or disassembly views. There’s a simpler option though.

Simple Option

Disable optimization locally for a few functions

Turn off the optimizer locally. On MSVC You can turn off the optimizer selectively via #pragma.

// Optimized code

// FUBAR code below, turn off the optimizer for debugging
#pragma optimize( "", off )

void foo()
  // Code you want to step through

void bar()
  // More code you want to step through

// Reenable optimization for everything else
#pragma optimize( "", on )

And now, you can step the debugger minus optimizer obfuscation.

Unreal Engine

If you’re working on an Unreal Engine based project, UE gives you a more platform independent-ish pair of macros:

void foo()
  // Code you want to step through

For MSVC these map to the pragmas cited above, other platforms & compilers will be different.


_Pragma("clang optimize off")
_Pragma("clang optimize on")

Next Up

I have an Amaze Your Friends and Render Orbit Lines Beautifully and Performantly by Perspectively-Projecting Conic Sections to the Frustum Plane about ready to go and need to finish it off… But I’ve been bigly busy.

If you’re mathematically inclined, here’s a thorough PDF solution on perspectively projecting an ellipse authored by David Eberly:

The other conic sections - hyperbolas, etc - are just generalizations of this solution.

Comments are disabled. To share feedback, please send email, or join the discussion on Discord.