My understanding is that in general when you add a handle to the clipboard, the clipboard then owns the handle and you are not responsible for deleting it, and you should not delete it. This is what it says here: https://msdn.microsoft.com/en-us/library/windows/desktop/ms649051%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396
If SetClipboardData succeeds, the system owns the object identified by the hMem parameter. The application may not write to or free the data once ownership has been transferred to the system, but it can lock and read from the data until the CloseClipboard function is called. (The memory must be unlocked before the Clipboard is closed.) If the hMem parameter identifies a memory object, the object must have been allocated using the function with the GMEM_MOVEABLE flag.
And indeed I see lots of examples where people call GlobalAlloc(), put some text in the global handle, call SetClipboardData(), and then don't free the global handle, because the clipboard owns it.
But in the case of HBITMAP data, added via SetClipboardData(CF_BITMAP, hBitmap)
, I see lots of examples like these:
https://stackoverflow.com/a/7292773/384670
https://stackoverflow.com/a/28248531/384670
In these cases, the code does delete the HBITMAP after adding it to the clipboard.
Is there a difference between an HBITMAP handle and a GlobalAlloc() handle with respect to the clipboard? Is there something special about the CF_BITMAP that's an exception to the rule, and the clipboard copies the handle instead of owns it? Can you point me to official (MSDN) documentation that explains the difference?
Edit:
Here is one more example that involves both types of handle in one article: http://www.codeproject.com/Articles/42/All-you-ever-wanted-to-know-about-the-Clipboard
Notice that in the case of the bitmap the author specifically says:
//copy has been made on clipboard so we can delete