zed-industries/zed · error
Error when registering clipboard format: {}
Error message
Error when registering clipboard format: {} What it means
On Windows, gpui registers private clipboard formats (PNG, GIF, JPG, SVG) with RegisterClipboardFormatW so copied images keep their type information. That Win32 call almost never fails for fixed, valid names; if it returns 0 the system could not allocate a new format (resource exhaustion or a damaged session). gpui turns the failure into a panic during static initialization of the clipboard format map.
Source
Thrown at crates/gpui_windows/src/clipboard.rs:52
LazyLock::new(|| register_clipboard_format(windows::core::w!("GIF")));
static CLIPBOARD_PNG_FORMAT: LazyLock<u32> =
LazyLock::new(|| register_clipboard_format(windows::core::w!("PNG")));
static CLIPBOARD_JPG_FORMAT: LazyLock<u32> =
LazyLock::new(|| register_clipboard_format(windows::core::w!("JFIF")));
static IMAGE_FORMATS_MAP: LazyLock<FxHashMap<u32, ImageFormat>> = LazyLock::new(|| {
let mut map = FxHashMap::default();
map.insert(*CLIPBOARD_PNG_FORMAT, ImageFormat::Png);
map.insert(*CLIPBOARD_GIF_FORMAT, ImageFormat::Gif);
map.insert(*CLIPBOARD_JPG_FORMAT, ImageFormat::Jpeg);
map.insert(*CLIPBOARD_SVG_FORMAT, ImageFormat::Svg);
map
});
fn register_clipboard_format(format: PCWSTR) -> u32 {
let ret = unsafe { RegisterClipboardFormatW(format) };
if ret == 0 {
panic!(
"Error when registering clipboard format: {}",
std::io::Error::last_os_error()
);
}
log::debug!(
"Registered clipboard format {} as {}",
unsafe { format.display() },
ret
);
ret
}
fn get_clipboard_data(format: u32) -> Option<LockedGlobal> {
let global = HGLOBAL(unsafe { GetClipboardData(format).ok() }?.0);
LockedGlobal::lock(global)
}
pub(crate) fn write_to_clipboard(item: ClipboardItem) {View on GitHub (pinned to f4178619ac)
Solutions
- Reboot to reset clipboard-format tables and GDI resources, then retry the app
- Use Task Manager (Details tab, add 'GDI objects' column) to find processes leaking GDI/user objects and close them
- If it persists, run sfc /scannow to rule out system-file corruption
- Capture the io::Error text from the panic message and report upstream if reproducible
Defensive patterns
Strategy: fallback
Try / catch
// isolate the first clipboard use so failed format registration degrades to text-only
let clipboard = std::panic::catch_unwind(|| clipboard_module::formats());
match clipboard {
Ok(formats) => enable_image_clipboard(formats),
Err(_) => log::error!("clipboard format registration failed; falling back to text-only clipboard"),
} Prevention
- Monitor GDI object counts when running long-lived Windows processes; leaks in any app degrade the whole session
- Treat clipboard-format registration failure as environmental: reboot and check system health before debugging your code
- Keep an image copy/paste smoke test in QA so this surfaces before production
When it happens
Trigger: First use of gpui's Windows clipboard code (copy/paste involving images) when RegisterClipboardFormatW returns 0 - typically exhausted internal GDI/kernel resources or a corrupted user session, since the format names are compile-time constants.
Common situations: Systems with severe GDI-object leaks in other long-running processes; broken terminal-services sessions; rare enough that a repeat occurrence points at system-level damage rather than app code.
Related errors
- Device lost: {err}
- RealtimeAudio priority should use spawn_realtime, not dispat
- ShellExecuteW failed to launch elevated process (code: {resu
- Device lost: {err}
- run request is missing a 'benchmark' block
AI-assisted analysis of zed-industries/zed@f4178619ac (2026-08-20).
Data as JSON: /api/errors/5e926fa099a19aef.
Report an issue: GitHub.