{"record":{"id":"8c20c51a0077a81f","repo":"GraphiteEditor/Graphite","slug":"createwindowexw-failed","errorCode":null,"errorMessage":"CreateWindowExW failed","messagePattern":"CreateWindowExW failed","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"desktop/src/window/win/native_handle.rs","lineNumber":71,"sourceCode":"\t\tlet style = WS_POPUP;\n\t\tlet helper = unsafe {\n\t\t\tCreateWindowExW(\n\t\t\t\tex,\n\t\t\t\tPCWSTR(HELPER_CLASS_NAME.encode_utf16().collect::<Vec<_>>().as_ptr()),\n\t\t\t\tPCWSTR::null(),\n\t\t\t\tstyle,\n\t\t\t\t0,\n\t\t\t\t0,\n\t\t\t\t0,\n\t\t\t\t0,\n\t\t\t\tSome(main),\n\t\t\t\tNone,\n\t\t\t\tNone,\n\t\t\t\t// Pass the main window's HWND to WM_NCCREATE so the helper can store it.\n\t\t\t\tSome(&main as *const _ as _),\n\t\t\t)\n\t\t}\n\t\t.expect(\"CreateWindowExW failed\");\n\n\t\t// Subclass the main window.\n\t\t// https://learn.microsoft.com/windows/win32/api/winuser/nf-winuser-setwindowlongptra\n\t\tlet prev_window_message_handler = unsafe { SetWindowLongPtrW(main, GWLP_WNDPROC, main_window_handle_message as *const () as isize) };\n\t\tif prev_window_message_handler == 0 {\n\t\t\tlet _ = unsafe { DestroyWindow(helper) };\n\t\t\tpanic!(\"SetWindowLongPtrW failed\");\n\t\t}\n\n\t\tlet native_handle = NativeWindowHandle {\n\t\t\tmain,\n\t\t\thelper,\n\t\t\tprev_window_message_handler,\n\t\t\tstate: Arc::new(Mutex::new(NativeWindowState::default())),\n\t\t};\n\t\tregistry::insert(&native_handle);\n\n\t\t// Place the helper over the main window and show it without activation.","sourceCodeStart":53,"sourceCodeEnd":89,"githubUrl":"https://github.com/GraphiteEditor/Graphite/blob/c507b356453361e31638b8bff8f6d46b6da2961e/desktop/src/window/win/native_handle.rs#L53-L89","documentation":"Panic when the Win32 CreateWindowExW call returns a null HWND. In this code the window being created is the helper window used to subclass the main window (the main window's HWND is passed through WM_NCCREATE so the helper can store it). A null return means the OS refused window creation: the window class was not registered, invalid parameters, exhaustion of USER/GDI handles, or broken session state.","triggerScenarios":"The window class for the helper window was never registered or registration failed silently earlier; USER/GDI handle exhaustion in the process or session (handle leak in the app or other apps); running in a broken or disconnected RDP session; out of memory for window objects.","commonSituations":"Long-running sessions where a leaking application exhausted the ~10k USER handle quota per session; remote desktop reconnects leaving stale session state; antivirus or shell-replacement software interfering with window creation; low system resources at startup.","solutions":["Capture GetLastError at the failure site (or via a debugger) to get the exact Win32 error code (ERROR_CLASS_DOES_NOT_EXIST, ERROR_NOT_ENOUGH_MEMORY, etc.)","Check handle usage in Task Manager (Details tab, add USER objects/GDI objects columns) and close leaking applications; reboot clears session handle leaks","Verify the class registration code path always runs before CreateWindowExW for both main and helper windows","Reconnect the RDP session or reboot if session state is suspected corrupt"],"exampleFix":"// before\n}\n.expect(\"CreateWindowExW failed\");\n\n// after\nlet hwnd = { /* CreateWindowExW(...) */ };\nif hwnd.is_invalid() {\n\tlet err = unsafe { GetLastError() };\n\tpanic!(\"CreateWindowExW failed with Win32 error {err}\");\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"let hwnd = unsafe { /* CreateWindowExW(...) */ };\nif hwnd.is_invalid() {\n\tlet code = unsafe { windows::Win32::Foundation::GetLastError() };\n\tpanic!(\"CreateWindowExW failed with Win32 error {}\", code.0);\n}","preventionTips":["Always register the window class and check its result before CreateWindowExW","Monitor USER/GDI object counts for leaks in long-running processes","Include GetLastError in the panic message; the raw message hides the OS reason"],"tags":["rust","win32","window-creation","windows","subclassing"],"backgroundTag":"win32-createwindow-failed","analyzedSha":"c507b356453361e31638b8bff8f6d46b6da2961e","analyzedAt":"2026-08-16T21:57:18.596Z","schemaVersion":2},"datasetVersion":"2026-08-16T23:17:17.608Z"}