{"record":{"id":"d2c03f7866b3f3b5","repo":"zed-industries/zed","slug":"globallock-returned-null","errorCode":null,"errorMessage":"GlobalLock returned null","messagePattern":"GlobalLock returned null","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/gpui_windows/src/clipboard.rs","lineNumber":154,"sourceCode":"        let filename_length = unsafe { DragQueryFileW(hdrop, file_index, None) } as usize;\n        let mut buffer = vec![0u16; filename_length + 1];\n        let ret = unsafe { DragQueryFileW(hdrop, file_index, Some(buffer.as_mut_slice())) };\n        if ret == 0 {\n            log::error!(\"unable to read file name of dragged file\");\n            continue;\n        }\n        match String::from_utf16(&buffer[0..filename_length]) {\n            Ok(file_name) => f(file_name),\n            Err(e) => log::error!(\"dragged file name is not UTF-16: {}\", e),\n        }\n    }\n}\n\nfn set_clipboard_bytes<T>(data: &[T], format: u32) -> Result<()> {\n    unsafe {\n        let global = Owned::new(GlobalAlloc(GMEM_MOVEABLE, std::mem::size_of_val(data))?);\n        let ptr = GlobalLock(*global);\n        anyhow::ensure!(!ptr.is_null(), \"GlobalLock returned null\");\n        std::ptr::copy_nonoverlapping(data.as_ptr(), ptr as _, data.len());\n        GlobalUnlock(*global).ok();\n        SetClipboardData(format, Some(HANDLE(global.0)))?;\n        // SetClipboardData succeeded — the system now owns the memory.\n        std::mem::forget(global);\n    }\n    Ok(())\n}\n\nfn get_clipboard_string(format: u32) -> Option<String> {\n    let locked = get_clipboard_data(format)?;\n    let bytes = locked.as_bytes();\n    let words_len = bytes.len() / std::mem::size_of::<u16>();\n    if words_len == 0 {\n        return Some(String::new());\n    }\n    let slice = unsafe { std::slice::from_raw_parts(bytes.as_ptr() as *const u16, words_len) };\n    let actual_len = slice.iter().position(|&c| c == 0).unwrap_or(words_len);","sourceCodeStart":136,"sourceCodeEnd":172,"githubUrl":"https://github.com/zed-industries/zed/blob/f4178619acd0d47ea1f76a2025c42962c6d6638c/crates/gpui_windows/src/clipboard.rs#L136-L172","documentation":"Sentinel guard in `set_clipboard_bytes` (called by write_string/write_image) raised when the Win32 `GlobalLock` on the freshly allocated global memory returns null, meaning the HGLOBAL could not be locked to copy clipboard payload into it (typically out-of-memory or an invalid/zeroed handle).","triggerScenarios":"Thrown at crates/gpui_windows/src/clipboard.rs:154 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Retry the GlobalAlloc/GlobalLock sequence; transient low-memory conditions can clear.","Check GlobalAlloc's return before locking to avoid locking an invalid handle.","Reduce the payload size (large images) if memory pressure is the cause.","Free the failed HGLOBAL with GlobalFree to avoid a leak before returning the error."],"exampleFix":null,"handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"f4178619acd0d47ea1f76a2025c42962c6d6638c","analyzedAt":"2026-08-20T19:29:52.058Z","contentChangedAt":"2026-08-20T19:29:52.058Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}