{"record":{"id":"54a33ef21eac01b0","repo":"SubtitleEdit/subtitleedit","slug":"failed-to-empty-clipboard","errorCode":null,"errorMessage":"Failed to empty clipboard","messagePattern":"Failed to empty clipboard","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/ui/Logic/ClipboardHelper.cs","lineNumber":269,"sourceCode":"                }\n                finally\n                {\n                    GlobalUnlock(hGlobal);\n                }\n\n                // Open clipboard and set data\n                if (!OpenClipboard(IntPtr.Zero))\n                {\n                    GlobalFree(hGlobal);\n                    throw new InvalidOperationException(\"Failed to open clipboard\");\n                }\n\n                try\n                {\n                    if (!EmptyClipboard())\n                    {\n                        GlobalFree(hGlobal);\n                        throw new InvalidOperationException(\"Failed to empty clipboard\");\n                    }\n\n                    if (SetClipboardData(CF_DIB, hGlobal) == IntPtr.Zero)\n                    {\n                        GlobalFree(hGlobal);\n                        throw new InvalidOperationException(\"Failed to set clipboard data\");\n                    }\n\n                    // Clipboard now owns the memory\n                    hGlobal = IntPtr.Zero;\n                }\n                finally\n                {\n                    CloseClipboard();\n                }\n            }\n            catch (Exception ex)\n            {","sourceCodeStart":251,"sourceCodeEnd":287,"githubUrl":"https://github.com/SubtitleEdit/subtitleedit/blob/17a9f0748781032255db3526b7215d2fb891e3af/src/ui/Logic/ClipboardHelper.cs#L251-L287","documentation":"Thrown in CopyImageToClipboardWindows after a successful OpenClipboard when EmptyClipboard() returns false. EmptyClipboard clears the current clipboard contents before setting new data; a failure is rare and indicates the clipboard was opened in a read-only/locked context or a low-level clipboard driver rejected the clear. The handle is freed before throwing.","triggerScenarios":"EmptyClipboard fails despite an open clipboard: clipboard locked by a viewer chain that refuses the clear, a clipboard redirector (RDP/VM) in a bad state, or the open handle being invalid by the time EmptyClipboard runs.","commonSituations":"RDP/VM clipboard redirection in a transient error state; a clipboard viewer holding the chain; antivirus blocking clipboard modification; opening the clipboard without owning a window (hWndNewOwner = Zero) under certain sessions.","solutions":["Pass a real window handle to OpenClipboard (non-Zero owner) instead of IntPtr.Zero where possible.","Check Marshal.GetLastWin32Error() for the specific failure.","Retry the open/empty sequence; disable clipboard viewers/redirectors and retry.","Ensure no other process is mid-clipboard-operation."],"exampleFix":"// before\nif (!EmptyClipboard()) { GlobalFree(hGlobal); throw new InvalidOperationException(\"Failed to empty clipboard\"); }\n\n// after - richer error and pass a window owner upstream\nif (!EmptyClipboard())\n{\n    var winErr = Marshal.GetLastWin32Error();\n    GlobalFree(hGlobal);\n    throw new InvalidOperationException($\"Failed to empty clipboard (Win32 error {winErr}).\");\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { if (!EmptyClipboard()) throw new InvalidOperationException($\"Failed to empty clipboard (Win32 {Marshal.GetLastWin32Error()}).\"); }\ncatch (InvalidOperationException) { GlobalFree(hGlobal); throw; }","preventionTips":["Pass a real window owner to OpenClipboard rather than IntPtr.Zero.","Retry the open/empty sequence under clipboard contention.","Disable clipboard redirectors in a bad state."],"tags":["clipboard","windows","win32"],"backgroundTag":null,"analyzedSha":"17a9f0748781032255db3526b7215d2fb891e3af","analyzedAt":"2026-08-13T18:11:43.374Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}