Search code examples
c++visual-studio-2010buildreleasehwnd

Value not assigned to handle when build using Release Configuration in VS2010


I use VS2010 and I have encountered following problem:

HWND handle = NULL;
handle = pPlatform->getWindowHandle(); 

When I debug this code in debug configuration a correct value is assigned to "handle", but when I debug this in release build "handle" stays NULL.

getWindowHandle() is just a simple accessor:

HWND PlatformManager::getWindowHandle()
{ 
   return windowHandle;
}

"windowHandle" has a non-NULL value both when debugging in debug/release build.

Thanks for the help.


Solution

  • The Release build includes optimizations, and sometimes the debugger might see the wrong value for variables that get optimized. It makes Release builds harder to debug, but that's exactly why they're not called Debug builds. :)

    In short, if you just have to debug a release build but your debugger is playing tricks on you, you can always resort to good-old printf debugging. Add a few trace functions, e.g. with OutputDebugString, and see if handle really stays NULL at that point.

    char buf[256]; HWND handle = NULL; handle = pPlatform->getWindowHandle(); OutputDebugStringA(_itoa((int)handle, buf, 10));