@paweld, The code seems a bit unstable.
Pressing Ctrl+C a few times, sometimes the clipboard is empty, and sometimes it gives an error (can't reproduce right now).
But I don't understand why...

I think it is because Lazarus doesn't play well with the Windows clipboard.
It uses an internal clipboard too.
When using CopyToClipboard in the TRichMemo it will use the actual Windows clipboard.
Lazarus clears that clipboard and puts it's own cached clipboard on it.
But because the TRichMemo didn't use the Lazarus clipboard, it's copied data is deleted.
The code of paweld does a Windows copy to clipboard but then reads that data (because it then has multiple formats) into the cached clipboard of Lazarus (via a list). After that the clipboard is owned by Lazarus and contains the copied data. And Lazarus leaves its own cached clipboard on the Windows clipboard on exit.
So, like I said as I showed the source code section, the "CLEAR" is being called when the clipbrd unit finalizes.
Actually, that part only clears the internal cached clipboard of Lazarus. When you trace through the code you will notice the Windows clipboard is already empty before that line. You can comment the Clear out in TClipboard.Destroy and it will still delete the Windows clipboard.
The problem lies somewhere in that TClipboard (when used for the first time) takes ownership of the Windows clipboard (TClipboard.GetOwnerShip).
After that Lazarus owns the clipboard and replaces everything on it with it's own cached version. I think it does this also on exit (when it still owns the clipboard). See the ClipboardGetOwnerShip() in TClipboard.Destroy. Although, commenting out that line isn't sufficient.
Anyway, I think it is a real bug that Lazarus just deletes the Windows clipboard on exit (even though it was owned by the program itself).
It's lucky it doesn't touch the clipboard if another program has copied something to the clipboard because that would be really
really wrong.
The TClipboard in clipbrd.pp has an internal cache system, so that an application can use all types of clipboards even if the underlying
platform does not support it.
BTW. It would also be possible to change TRichMemo to not use the Windows clipboard directly but the Lazarus clipboard functionality. That would fix it too (although I don't like this very much). It would be better to have an option to eliminate the Lazarus clipboard completely. At least that caching and replacing thing.