should i free the memory i copied to clipboard?

这一生的挚爱 提交于 2021-01-27 14:19:35

问题


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.


回答1:


It is generally done by the system however, some responsible applications also take care of asking the user to free up the clipboard before leaving.

e.g. MSWord will ask the user to keep the data in memory or not before it quits. For general purpose, you can leave it to system.

Remember here that user might want to keep it in Clipboard (Even after closing application) so you should not mangle with it and remove it from memory.




回答2:


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.




回答3:


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.




回答4:


Your application is responsible for handling data on the clipboard, if it put it there. This is why a lot of applications, like Microsoft Office, ask you if you want to keep a large amount of data on the clipboard or not, when you exit the application.

I would strongly advise user-interaction however, since you do not know if the user needs the data on the clipboard somewhere else later.



来源:https://stackoverflow.com/questions/16036410/should-i-free-the-memory-i-copied-to-clipboard

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!