{"record":{"id":"5e926fa099a19aef","repo":"zed-industries/zed","slug":"error-when-registering-clipboard-format","errorCode":null,"errorMessage":"Error when registering clipboard format: {}","messagePattern":"Error when registering clipboard format: (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/gpui_windows/src/clipboard.rs","lineNumber":52,"sourceCode":"    LazyLock::new(|| register_clipboard_format(windows::core::w!(\"GIF\")));\nstatic CLIPBOARD_PNG_FORMAT: LazyLock<u32> =\n    LazyLock::new(|| register_clipboard_format(windows::core::w!(\"PNG\")));\nstatic CLIPBOARD_JPG_FORMAT: LazyLock<u32> =\n    LazyLock::new(|| register_clipboard_format(windows::core::w!(\"JFIF\")));\n\nstatic IMAGE_FORMATS_MAP: LazyLock<FxHashMap<u32, ImageFormat>> = LazyLock::new(|| {\n    let mut map = FxHashMap::default();\n    map.insert(*CLIPBOARD_PNG_FORMAT, ImageFormat::Png);\n    map.insert(*CLIPBOARD_GIF_FORMAT, ImageFormat::Gif);\n    map.insert(*CLIPBOARD_JPG_FORMAT, ImageFormat::Jpeg);\n    map.insert(*CLIPBOARD_SVG_FORMAT, ImageFormat::Svg);\n    map\n});\n\nfn register_clipboard_format(format: PCWSTR) -> u32 {\n    let ret = unsafe { RegisterClipboardFormatW(format) };\n    if ret == 0 {\n        panic!(\n            \"Error when registering clipboard format: {}\",\n            std::io::Error::last_os_error()\n        );\n    }\n    log::debug!(\n        \"Registered clipboard format {} as {}\",\n        unsafe { format.display() },\n        ret\n    );\n    ret\n}\n\nfn get_clipboard_data(format: u32) -> Option<LockedGlobal> {\n    let global = HGLOBAL(unsafe { GetClipboardData(format).ok() }?.0);\n    LockedGlobal::lock(global)\n}\n\npub(crate) fn write_to_clipboard(item: ClipboardItem) {","sourceCodeStart":34,"sourceCodeEnd":70,"githubUrl":"https://github.com/zed-industries/zed/blob/f4178619acd0d47ea1f76a2025c42962c6d6638c/crates/gpui_windows/src/clipboard.rs#L34-L70","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":null,"handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":"// isolate the first clipboard use so failed format registration degrades to text-only\nlet clipboard = std::panic::catch_unwind(|| clipboard_module::formats());\nmatch clipboard {\n    Ok(formats) => enable_image_clipboard(formats),\n    Err(_) => log::error!(\"clipboard format registration failed; falling back to text-only clipboard\"),\n}","preventionTips":["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"],"tags":["windows","clipboard","win32","gpui"],"backgroundTag":"win32-api-call-failed","analyzedSha":"f4178619acd0d47ea1f76a2025c42962c6d6638c","analyzedAt":"2026-08-20T19:29:52.058Z","contentChangedAt":"2026-08-20T19:29:52.058Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}