Search code examples
visual-c++mfcstatusbarcode-analysis

Warning C6387 when using SetIcon


Code:

m_hStatusBarZoomFactorIcon =
        CMeetingScheduleAssistantApp::HICONFromCBitmap(
m_mapMenuBitmap[MAKEWPARAM(MenuNavigation::View, SubMenuPos::Zoom)]);
    VERIFY(m_hStatusBarZoomFactorIcon);
    m_StatusBar.GetStatusBarCtrl().SetIcon(
        to_underlying(StatusBarPane::ProgressOrZoomFactor), m_hStatusBarZoomFactorIcon);

The variables are HICON and CStatusBar respectively. Why am I getting this warning?

Warning C6387 m_hStatusBarZoomFactorIcon could be 0: this does not adhere to the specification for the function CStatusBarCtrl::SetIcon.


Solution

  • You should add code to check that m_hStatusBarZoomFactorIcon is valid (i.e. not null) before calling the SetIcon() function with that as a parameter:

    m_hStatusBarZoomFactorIcon =
        CMeetingScheduleAssistantApp::HICONFromCBitmap(
            m_mapMenuBitmap[MAKEWPARAM(MenuNavigation::View, SubMenuPos::Zoom)]);
    //  VERIFY(m_hStatusBarZoomFactorIcon); // vide infra
        if (m_hStatusBarZoomFactorIcon) { // Only use it if it's valid ...
            m_StatusBar.GetStatusBarCtrl().SetIcon(
                to_underlying(StatusBarPane::ProgressOrZoomFactor), 
                m_hStatusBarZoomFactorIcon);
        }
        else {
            // Error handling code
        }
    

    Note: If you were relying on the VERIFY(m_hStatusBarZoomFactorIcon); statement to take care of that check then note that that expands to effectively nothing unless the _DEBUG macro is also defined (i.e. in debug builds). From afx.h:

    //...
    #else   // _DEBUG
    
    #define VERIFY(f)          ((void)(f))
    ///