3

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.

user1544067
  • 1,676
  • 2
  • 19
  • 30

4 Answers4

7

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.

Raymond Chen
  • 44,448
  • 11
  • 96
  • 135
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.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
1

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.

John Willemse
  • 6,608
  • 7
  • 31
  • 45
  • 1
    It depends on how the data was put there. If put there via `SetClibpoardData` and a non-`NULL` data parameter, then the operating system is responsible for the data, not the application. "[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](http://msdn.microsoft.com/en-us/library/windows/desktop/ms649051(v=vs.85).aspx)." – Raymond Chen Apr 16 '13 at 14:36
  • Thanks Raymond, I didn't know that. Big fan of your blog BTW ;) – John Willemse Apr 16 '13 at 14:41
0

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.

Murtuza Kabul
  • 6,438
  • 6
  • 27
  • 34
  • "You can leave it to the system" when the system will do absolutely nothing, except when shutting down or restarting. An application is responsible for the data it puts on the clipboard, not the system. – John Willemse Apr 16 '13 at 12:32
  • @John Willemse: Yes it is true but even you should not do anything with it as it is owned by user and it is user who will decide if he/she wants to keep it in memory or not. – Murtuza Kabul Apr 16 '13 at 12:42
  • @John Willemse: Apart from Microsoft office, list an application which take care of cleaning up clipboard or asking the user to do so. You would hardly find a few and it is so for reason. – Murtuza Kabul Apr 16 '13 at 12:45
  • You don't get my point. I agree that you should generally leave it on the clipboard, as per my answer. But if your application puts a _large_ amount of data on the clipboard, it is advisable to query the user if he wants to keep it there. The system will _not_ clean it up automatically, even though it may impact performance. – John Willemse Apr 16 '13 at 12:49