{"record":{"id":"64c20f9cb5528785","repo":"slint-ui/slint","slug":"c-nativewindowhandle-created-with-null-hwnd","errorCode":null,"errorMessage":"C++: NativeWindowHandle created with null hwnd","messagePattern":"C\\+\\+: NativeWindowHandle created with null hwnd","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"api/cpp/platform.rs","lineNumber":794,"sourceCode":"\n    struct CppRawHandle(Arc<RawHandlePair>);\n\n    impl From<(RawWindowHandle, RawDisplayHandle)> for CppRawHandle {\n        fn from(pair: (RawWindowHandle, RawDisplayHandle)) -> Self {\n            Self(Arc::new(RawHandlePair(pair)))\n        }\n    }\n\n    type CppRawHandleOpaque = *const c_void;\n\n    #[unsafe(no_mangle)]\n    pub unsafe extern \"C\" fn slint_new_raw_window_handle_win32(\n        hwnd: *mut c_void,\n        _hinstance: *mut c_void,\n    ) -> CppRawHandleOpaque {\n        let handle = CppRawHandle::from((\n            RawWindowHandle::Win32(raw_window_handle::Win32WindowHandle::new(\n                (hwnd as isize).try_into().expect(\"C++: NativeWindowHandle created with null hwnd\"),\n            )),\n            RawDisplayHandle::Windows(raw_window_handle::WindowsDisplayHandle::new()),\n        ));\n        Box::into_raw(Box::new(handle)) as CppRawHandleOpaque\n    }\n\n    #[unsafe(no_mangle)]\n    pub unsafe extern \"C\" fn slint_new_raw_window_handle_x11_xcb(\n        window: u32,\n        visual_id: u32,\n        connection: *mut c_void,\n        screen: core::ffi::c_int,\n    ) -> CppRawHandleOpaque {\n        use raw_window_handle::{XcbDisplayHandle, XcbWindowHandle};\n        let handle = CppRawHandle::from((\n            RawWindowHandle::Xcb({\n                let mut hnd = XcbWindowHandle::new(\n                    window","sourceCodeStart":776,"sourceCodeEnd":812,"githubUrl":"https://github.com/slint-ui/slint/blob/bb937076de3f7919766c1f25e2e969367cf77e9a/api/cpp/platform.rs#L776-L812","documentation":"The C++ API's slint_new_raw_window_handle_win32 converts a raw HWND pointer into a raw-window-handle Win32WindowHandle, whose NonNull HWND field is constructed via isize::try_into().expect(...). A null HWND (0) cannot be converted to NonNull, so the library panics with this message rather than producing an invalid window handle. It is a hard invariant: a window handle wrapper is meaningless without a valid window.","triggerScenarios":"Calling slint_native_window_handle_from_raw / the C++ NativeWindowHandle constructor with an uninitialized or already-destroyed Win32 window's HWND (nullptr), or default-constructed zero handle passed from C++ code.","commonSituations":"Passing a window handle captured before CreateWindow completed, using an HWND after the window was destroyed, or wiring up Win32 interop where the platform layer returned nullptr (e.g. failed window creation).","solutions":["Check the HWND is non-null before calling the Slint API that wraps it.","Fix the window-creation path so a valid HWND exists before registering it (verify CreateWindowEx returned non-null).","Reorder initialization: create the native window first, then construct Slint's NativeWindowHandle.","If the window was destroyed, recreate it and re-create the handle instead of reusing a stale pointer."],"exampleFix":"// before (C++)\nHWND hwnd = nullptr; // not yet created\nauto handle = slint::NativeWindowHandle::from_win32(hwnd, hinstance);\n// after\nHWND hwnd = create_native_window();\nif (!hwnd) throw std::runtime_error(\"window creation failed\");\nauto handle = slint::NativeWindowHandle::from_win32(hwnd, hinstance);","handlingStrategy":"validation","validationCode":"// C++ caller, before constructing the handle\nif (hwnd == nullptr) {\n    throw std::invalid_argument(\"cannot create NativeWindowHandle: null HWND\");\n}","typeGuard":"// C++ helper\nbool is_valid_hwnd(HWND hwnd) { return hwnd != nullptr && IsWindow(hwnd) == TRUE; }","tryCatchPattern":null,"preventionTips":["Create and verify the native window before wrapping it in a Slint handle.","Check CreateWindowEx's return for null before proceeding.","Never cache HWNDs across window destroy/recreate cycles.","Assert non-null HWNDs at API boundaries in your own code with early failure.","Guard against platform layers (e.g. embedded webviews) that return null handles on failure."],"tags":["cpp","win32","window-handle","panic","null-pointer"],"backgroundTag":"null-argument","analyzedSha":"bb937076de3f7919766c1f25e2e969367cf77e9a","analyzedAt":"2026-09-16T01:37:20.251Z","contentChangedAt":"2026-09-16T01:37:20.251Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}