Search code examples
winapiiconsgdi

Should we delete hbmColor & hbmMask for a shared icon?


I have a shared icon HICON from the following snippet.

::SendMessageTimeout(hwnd, WM_GETICON, ICON_SMALL, 0, SMTO_ABORTIFHUNG, 500, &icon)

Then I retrieved its ICONINFO and copied out the hbmColor and hbmMask. As I notice in other snippets, people then call ::DeleteObject() to destroy hbmColor and hbmMask.

Is that also valid for a shared icon?

As mentioned in MSDN, we should not call DestroyIcon on a shared icon, which sounds like we should not call DestroyObject on a shared icon's hbmColor & hbmMask either.

It is only necessary to call DestroyIcon for icons and cursors created with the following functions: CreateIconFromResourceEx (if called without the LR_SHARED flag), CreateIconIndirect, and CopyIcon.


Solution

  • The documentation for GetIconInfo says:

    GetIconInfo creates bitmaps for the hbmMask and hbmColor members of ICONINFO. The calling application must manage these bitmaps and delete them when they are no longer necessary.

    This is true equally for shared icons. You will have to delete the bitmaps when you are finished with them.