question

dhkim-5089 avatar image
0 Votes"
dhkim-5089 asked RLWA32-6355 edited

[Clipboard] Do I need to call GlobalFree when using setClipboardData ?

in the code below, When GlobalFree is called, an error occurs in GlobalAlloc when GetClipboardData is called.

MSDN doesn't mention GlobalFree when calling SetClipboardData.

 if (OpenClipboardEx(m_hViewer))
 {
        
     hglbCopy = GlobalAlloc(GMEM_MOVEABLE,    
        
     lptstrCopy = (LPTSTR)GlobalLock(hglbCopy);
     memcpy(lptstrCopy, &szTest, cch * sizeof(TCHAR));
     lptstrCopy[cch] = (TCHAR)0;   
     GlobalUnlock(hglbCopy);
        
     ::SetClipboardData(CF_UNICODETEXT, hglbCopy);
     CloseClipboard();
    **GlobalFree(hglbCopy);**

}



So when GloballAlloc is called, I thought that GlobalFree calls were natural.

Some Internet articles say that GlobalFree should not be called, but I can't see any official information related to MSDN.

If you know anything about this, please reply.

thank you.


My previous question
clipboard-getclipboard-globallock-fail-1.html







c++
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

RLWA32-6355 avatar image
0 Votes"
RLWA32-6355 answered RLWA32-6355 edited

No, do not call GlobalFree after putting data on the clipboard. Think about it this way -- you are transferring ownership of the global memory block from your application to the clipboard. When the clipboard no longer needs the data, it will release the memory by calling GlobalFree itself.

Official documentation from SetClipboardData function -- "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..."


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.