Icon-handle ownership
Whether to call DestroyIcon depends on whether the icon is a shared resource or a resource that the caller owns and must release. Destroying a shared icon can affect other code that uses the same resource.
When not to call DestroyIcon
- An icon returned by
LoadIconis shared. Do not destroy it withDestroyIcon.LoadIconloads an icon at the standard system metric size; useLoadImagewhen you need to specify a size. - When you pass
LR_SHAREDtoLoadImage, the handle is shared. Do not callDestroyIcon. Do not useLR_SHAREDfor images loaded from files or for nonstandard sizes. CopyImagecan return the original handle when you passLR_COPYRETURNORGand the inputhImageis a shared icon. Do not destroy that shared handle.
When to call DestroyIcon
Release caller-owned icons with DestroyIcon when you are finished using them. Microsoft gives these examples:
CreateIconFromResourceExwithoutLR_SHAREDCreateIconIndirectCopyIcon, which returns a duplicate icon handle- An icon loaded with
LoadImagewithoutLR_SHARED
A note about CreateIconFromResource
Microsoft’s general DestroyIcon page lists CreateIconFromResource as a source of shared icons, while the individual function pages say to call DestroyIcon. The CreateIconFromResource and CreateIconFromResourceEx pages also conflict internally about LR_SHARED and releasing the returned handle. Do not infer ownership for these APIs from the general rule alone; consult the current page for the specific function. This documentation conflict is separate from the shared-handle behavior of LoadIcon.
An icon passed to DestroyIcon must not be in use. Check whether a handle is shared and whether its source module is still loaded.