使用 SetIcon 时出现警告 C6387

Warning C6387 when using SetIcon

代码:

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

变量分别是HICONCStatusBar。为什么我会收到此警告?

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

您应该添加代码以检查 m_hStatusBarZoomFactorIcon 是否有效(即 not null),然后再调用 SetIcon() 函数并将其作为参数:

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
    }

注意:如果您依赖 VERIFY(m_hStatusBarZoomFactorIcon); 语句来处理该检查,请注意扩展为 有效 除非 _DEBUG还定义了宏(即在调试版本中)。来自 afx.h:

//...
#else   // _DEBUG

#define VERIFY(f)          ((void)(f))
///