Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

should i free the memory i copied to clipboard?

Tags:

memory

winapi

When I copy data in my win32 program to the clipbord, should i free the memory i copied to clipboard, after I paste it elsewhere? or the system is responsible for this.

like image 848
user1544067 Avatar asked Dec 04 '25 16:12

user1544067


2 Answers

There are two ways of putting data on the clipboard.

Method 1: Put the data onto the clipboard directly, by calling SetClipboardData and passing a non-NULL value as the second parameter. In that case, the system will take responsibility for the data, and you should not free it yourself.

Method 2: Put a placeholder onto the clipboard, by calling SetClipboardData and passing NULL as the second parameter. In that case, the application is responsible for the data until the point it calls SetClibpoardData with a non-NULL second parameter, at which point responsibility transfers to the operating system.

It's not clear from your question which method you are using.

like image 116
Raymond Chen Avatar answered Dec 07 '25 17:12

Raymond Chen


Read the documentation:

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

Keeping track of the clipboard data so you can remove it from the clipboard when closing your app is completely optional. Once the data is on the clipboard, the system owns it and it is separate from your app, so you can choose to leave it on the clipboard so it remains available for continued usage after your app is closed. Unless you are using delayed rendering, that ism in which case it does make sense to remove it from the clipboard when closing your app, since your app will not be running anymore to render the data when requested by other apps.

like image 44
Remy Lebeau Avatar answered Dec 07 '25 16:12

Remy Lebeau



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!