Home > Programming > AFXASSUME bug in VC 2005

AFXASSUME bug in VC 2005

December 13, 2008

I got a bug report recently about one of my open source modules, namely CVersionInfo giving a compiler warning when the code is compiled in release mode on VC 2005 as follows:

warning C4189: ‘__afx_condVal’ : local variable is initialized but not referenced

If you review the code that caused this warning you will see that it is the macro AFXASSUME. This macro which I have started using in all my code as I upgrade it to support VC 2005 and later only as well as Code Analysis aka Prefast combines the job of the MFC ASSERT macro and the Code Analysis “__analysis_assume” function. Well it turns out that Microsoft just didn’t get the macro fully correct on their first implementation in VC 2005, because they forgot to explicitly optimize the macro away in release mode code just like how the standard ASSERT works. Here’s AFXASSUME from VC 2005 with SP1 applied:

#define AFXASSUME(cond) do { bool __afx_condVal=!!(cond); ASSERT(__afx_condVal); __analysis_assume(__afx_condVal); } while(0)

As you can see it does not take the _DEBUG pre-processor macro into account.

This is reported as a bug on the connect web site at https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=101373. This page refers to the error when you build a standard MFC doc/view application with AppWizard and include support for CRichEditView view class. The page also includes a workaround to avoid the compiler warning.

If you take a look at AFX.h in VC 2008 (with SP1) you will see that it has been fixed for good:

#if defined(_PREFAST_) || defined (_DEBUG)
#define AFXASSUME(cond)            do { bool __afx_condVal=!!(cond); ASSERT(__afx_condVal); __analysis_assume(__afx_condVal); } while(0)
#else
#define AFXASSUME(cond)            ((void)0)
#endif

So if you are getting this error in VC 2005, than bear in mind that the problem may not be related to my code but could be a bug in MFC itself. With that said, if you want to release code which incorporates my code and your compiler is VC 2005, then here is a more comprehensive workaround which you should place before you include any of my source modules which use AFXASSUME. Normally the best place to put this would be in your precompiled header stdafx.h before you reference any of my modules:

#if (_MSC_VER < 1500)
  #ifdef AFXASSUME //Redefine the buggy version of AFXASSUME if we are being compiled on VC 2005 which generates the compiler warning "C4189: ‘__afx_condVal’ : local variable is initialized but not referenced". This bug was fixed in VC 2008
    #if defined(_PREFAST_) || defined (_DEBUG)
      #undef AFXASSUME
      #define AFXASSUME(cond)    do { bool __afx_condVal=!!(cond); ASSERT(__afx_condVal); __analysis_assume(__afx_condVal); } while(0)
    #else
      #undef AFXASSUME
      #define AFXASSUME(cond) ((void)0)
    #endif
  #endif
#endif

If the word wrapping seems a bit confusing above, then you can download the macro directly from http://www.naughter.com/download/FIX_AFXASSUME.h.

Happy coding!

Advertisements
Categories: Programming
  1. Unknown
    May 15, 2009 at 11:55 am

    Hi,Great blog with interesting informations. I can use it t solve my problem.ThanxM.http://www.vanjobb.hu/

  1. No trackbacks yet.
Comments are closed.
%d bloggers like this: