{"record":{"id":"ade1bfc1f28d42df","repo":"SubtitleEdit/subtitleedit","slug":"failed-to-open-clipboard","errorCode":null,"errorMessage":"Failed to open clipboard","messagePattern":"Failed to open clipboard","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/ui/Logic/ClipboardHelper.cs","lineNumber":261,"sourceCode":"                    // Write pixel data (flip vertically for bottom-up DIB)\n                    IntPtr pixelPtr = IntPtr.Add(ptr, headerSize);\n                    for (int y = 0; y < height; y++)\n                    {\n                        int sourceOffset = (height - 1 - y) * stride;\n                        IntPtr destPtr = IntPtr.Add(pixelPtr, y * stride);\n                        Marshal.Copy(pixels, sourceOffset, destPtr, stride);\n                    }\n                }\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;","sourceCodeStart":243,"sourceCodeEnd":279,"githubUrl":"https://github.com/SubtitleEdit/subtitleedit/blob/17a9f0748781032255db3526b7215d2fb891e3af/src/ui/Logic/ClipboardHelper.cs#L243-L279","documentation":"Thrown in CopyImageToClipboardWindows when OpenClipboard(IntPtr.Zero) returns false — the Windows clipboard could not be opened, typically because another application currently has it open. OpenClipboard is exclusive: only one window may have it open at a time. The allocated hGlobal is freed before throwing. This is one of the most common clipboard failures on Windows.","triggerScenarios":"Another process/window holds the clipboard open (clipboard viewers, remote-desktop/VM tools, screen-grabbers, office clipboard managers); retrying too quickly after a previous open; a process that opened the clipboard and stalled without closing it.","commonSituations":"Remote Desktop or a VM integration service holding the clipboard; a clipboard manager (ClipX, Ditto, Office); antivirus inspecting clipboard content; rapid successive copy operations.","solutions":["Retry OpenClipboard with small delays (e.g. up to N attempts, 50-100ms apart) before giving up, mirroring the SetTextAsync retry pattern.","Close other clipboard-viewer/manager applications and retry.","If running under RDP/VM, reconnect or disable clipboard redirection temporarily.","Check Marshal.GetLastWin32Error() (ERROR_ACCESS_DENIED = 5 confirms contention)."],"exampleFix":"// before\nif (!OpenClipboard(IntPtr.Zero)) { GlobalFree(hGlobal); throw new InvalidOperationException(\"Failed to open clipboard\"); }\n\n// after - bounded retry, same strategy as SetTextAsync\nbool opened = false;\nfor (int i = 0; i < 10 && !opened; i++) { opened = OpenClipboard(IntPtr.Zero); if (!opened) Thread.Sleep(50); }\nif (!opened) { GlobalFree(hGlobal); throw new InvalidOperationException($\"Failed to open clipboard (Win32 error {Marshal.GetLastWin32Error()}).\"); }","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"// Mirror the SetTextAsync retry: OpenClipboard is exclusive and commonly contended.\nbool opened = false;\nfor (int i = 0; i < 10 && !opened; i++) { opened = OpenClipboard(IntPtr.Zero); if (!opened) Thread.Sleep(50); }\nif (!opened) { GlobalFree(hGlobal); throw new InvalidOperationException($\"Failed to open clipboard (Win32 {Marshal.GetLastWin32Error()}).\"); }","preventionTips":["Retry OpenClipboard with short delays (it is exclusive).","Close clipboard viewers/managers/VM redirectors that hold it open.","Prefer passing a real window handle as the owner."],"tags":["clipboard","windows","win32","contention"],"backgroundTag":null,"analyzedSha":"17a9f0748781032255db3526b7215d2fb891e3af","analyzedAt":"2026-08-13T18:11:43.374Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}