SubtitleEdit/subtitleedit · error · InvalidOperationException

Failed to empty clipboard

Error message

Failed to empty clipboard

What it means

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.

Source

Thrown at src/ui/Logic/ClipboardHelper.cs:269

                }
                finally
                {
                    GlobalUnlock(hGlobal);
                }

                // Open clipboard and set data
                if (!OpenClipboard(IntPtr.Zero))
                {
                    GlobalFree(hGlobal);
                    throw new InvalidOperationException("Failed to open clipboard");
                }

                try
                {
                    if (!EmptyClipboard())
                    {
                        GlobalFree(hGlobal);
                        throw new InvalidOperationException("Failed to empty clipboard");
                    }

                    if (SetClipboardData(CF_DIB, hGlobal) == IntPtr.Zero)
                    {
                        GlobalFree(hGlobal);
                        throw new InvalidOperationException("Failed to set clipboard data");
                    }

                    // Clipboard now owns the memory
                    hGlobal = IntPtr.Zero;
                }
                finally
                {
                    CloseClipboard();
                }
            }
            catch (Exception ex)
            {

View on GitHub (pinned to 17a9f07487)

Solutions

  1. Pass a real window handle to OpenClipboard (non-Zero owner) instead of IntPtr.Zero where possible.
  2. Check Marshal.GetLastWin32Error() for the specific failure.
  3. Retry the open/empty sequence; disable clipboard viewers/redirectors and retry.
  4. Ensure no other process is mid-clipboard-operation.

Example fix

// before
if (!EmptyClipboard()) { GlobalFree(hGlobal); throw new InvalidOperationException("Failed to empty clipboard"); }

// after - richer error and pass a window owner upstream
if (!EmptyClipboard())
{
    var winErr = Marshal.GetLastWin32Error();
    GlobalFree(hGlobal);
    throw new InvalidOperationException($"Failed to empty clipboard (Win32 error {winErr}).");
}
Defensive patterns

Strategy: try-catch

Try / catch

try { if (!EmptyClipboard()) throw new InvalidOperationException($"Failed to empty clipboard (Win32 {Marshal.GetLastWin32Error()})."); }
catch (InvalidOperationException) { GlobalFree(hGlobal); throw; }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of SubtitleEdit/subtitleedit@17a9f07487 (2026-08-13). Data as JSON: /api/errors/54a33ef21eac01b0. Report an issue: GitHub.