简体   繁体   中英

Using _DEBUG Preprocessor Defintion in Visual Studio 2008 - C++

I have a VC++ application written using Visual Studio 2008. For debug purposes, timeout values compile differently depending on the type of build (Debug or Release). The code sample below is typical of how I am trying to do this.

#ifdef _DEBUG
if ( (dwObjectWaitState = ::WaitForSingleObject( m_hValidMsgRxdEvent, INFINITE )) != WAIT_OBJECT_0 )
#else
if ( (dwObjectWaitState = ::WaitForSingleObject( m_hValidMsgRxdEvent, BAS_THREE_SEC_TIMEOUT )) != WAIT_OBJECT_0 )
#endif
{
    /* ... */
}

The Debug configuration has _DEBUG defined in the Preprocessor Definitions. The Release configuration does not have it. For each respective configuration, the expected lined is grayed out (presumably to indicate that the other line will be compiled in).

However, at run-time, the Release build timeouts remain INFINITE . When I try to set a breakpoint on both if statements and try to run the Release code, the breakpoint on the first if statement remains, whereas the other breakpoint gets moved down to the first line inside the brackets.

What gives? How do I make this compile option work? Should I be using something else?

Something like this instead perhaps?

#ifdef _DEBUG
#define NONMS_WAIT_TIMEOUT INFINITE
#else
#define NONMS_WAIT_TIMEOUT BAS_THREE_SEC_TIMEOUT
#endif

if ( (dwObjectWaitState = ::WaitForSingleObject( m_hValidMsgRxdEvent, NONMS_WAIT_TIMEOUT)) != WAIT_OBJECT_0 )
{
    /* ... */
}

Edit: Debug builds in VS should have _DEBUG defined and release builds should have NDEBUG defined. Check your project pre-processor directives to make sure this is what you have.

Write it like this and build in the Release build:

#ifdef _DEBUG
#error Something is really wrong, _DEBUG is still defined
if ( (dwObjectWaitState = ::WaitForSingleObject( m_hValidMsgRxdEvent, INFINITE )) != WAIT_OBJECT_0 )
#else
if ( (dwObjectWaitState = ::WaitForSingleObject( m_hValidMsgRxdEvent, BAS_THREE_SEC_TIMEOUT )) != WAIT_OBJECT_0 )
#endif

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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