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
- 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.
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
- 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.
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
- Failed to allocate global memory
- Failed to lock global memory
- Failed to open clipboard
- Failed to set clipboard data
- Failed to decode bitmap
AI-assisted analysis of SubtitleEdit/subtitleedit@17a9f07487 (2026-08-13).
Data as JSON: /api/errors/54a33ef21eac01b0.
Report an issue: GitHub.