{"record":{"id":"170b51065cc09b57","repo":"stride3d/stride","slug":"devicewindowhandle-cannot-be-null","errorCode":null,"errorMessage":"DeviceWindowHandle cannot be null","messagePattern":"DeviceWindowHandle cannot be null","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/engine/Stride.Graphics/Direct3D/SwapChainGraphicsPresenter.Direct3D.cs","lineNumber":556,"sourceCode":"            DepthStencilBuffer.InitializeFrom(newTextureDescription);\n\n            foreach (var childTexture in childrenTextures)\n            {\n                childTexture.InitializeFrom(parentTexture: DepthStencilBuffer, in childTexture.ViewDescription);\n            }\n        }\n\n        /// <summary>\n        ///   Creates or reinitializes the Swap-Chain with the current configuration.\n        /// </summary>\n        /// <exception cref=\"InvalidOperationException\">\n        ///   <see cref=\"PresentationParameters.DeviceWindowHandle\"/> is <see langword=\"null\"/> or\n        ///   the <see cref=\"WindowHandle.Handle\"/> is invalid or zero.\n        /// </exception>\n        private void CreateSwapChain()\n        {\n            if (Description.DeviceWindowHandle is null)\n                throw new InvalidOperationException(\"DeviceWindowHandle cannot be null\");\n\n            // D3D presenters don't honor SurfaceRotation (DXGI handles rotation transparently).\n            if (SurfaceRotation != SurfaceRotation.Identity)\n                throw new NotImplementedException($\"SurfaceRotation '{SurfaceRotation}' is not supported on Direct3D presenters.\");\n\n#if STRIDE_PLATFORM_UWP\n            CreateSwapChainForUWP();\n#else\n            CreateSwapChainForWindows();\n#endif\n        }\n\n#if STRIDE_PLATFORM_UWP\n        /// <summary>\n        ///   Creates or reinitializes the Swap-Chain on the Universal Windows Platform (UWP).\n        /// </summary>\n        private void CreateSwapChainForUWP()\n        {","sourceCodeStart":538,"sourceCodeEnd":574,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/engine/Stride.Graphics/Direct3D/SwapChainGraphicsPresenter.Direct3D.cs#L538-L574","documentation":"SwapChainGraphicsPresenter.CreateSwapChain throws InvalidOperationException when PresentationParameters.DeviceWindowHandle is null, since a D3D swap chain must be created against a valid output window. The XML docs on the method explicitly state the handle must be non-null and its Handle non-zero. This fires in the constructor and in OnRecreated (swap-chain re-creation).","triggerScenarios":"Constructing SwapChainGraphicsPresenter (directly or via GraphicsDevice/presenter factory on Direct3D) with PresentationParameters whose DeviceWindowHandle was never set, or calling OnRecreated after the window was closed/disposed leaving the handle null.","commonSituations":"Running a Stride headless/offscreen setup on a D3D backend without assigning a window handle or using the proper offscreen presenter; forgetting to set GameWindow/window handle before graphics device init; window closed then device recreate triggered; copy-pasting presenter setup from non-windowed contexts.","solutions":["Set PresentationParameters.DeviceWindowHandle to a valid window (e.g. gameWindow.NativeWindow? or WindowHandle from your platform window) before creating the presenter/device","For offscreen rendering, use the appropriate offscreen/graphics-context presenter instead of a swap chain presenter","Guard window lifetime: don't trigger OnRecreated/device reinit after the window is disposed","Verify the handle's Handle value is non-zero before init"],"exampleFix":"// before\nvar parameters = new PresentationParameters(); // DeviceWindowHandle is null\nvar device = GraphicsDevice.New(null, parameters);\n// after\nvar parameters = new PresentationParameters\n{\n    DeviceWindowHandle = Game.Window.NativeWindow // must be a valid, live window handle\n};\nvar device = GraphicsDevice.New(null, parameters);","handlingStrategy":"validation","validationCode":"if (parameters.DeviceWindowHandle is null || parameters.DeviceWindowHandle.Handle == IntPtr.Zero)\n    throw new InvalidOperationException(\"Assign a valid DeviceWindowHandle before creating a D3D presenter.\");","typeGuard":"bool HasValidWindowHandle(PresentationParameters p) =>\n    p?.DeviceWindowHandle != null && p.DeviceWindowHandle.Handle != IntPtr.Zero;","tryCatchPattern":"try { new SwapChainGraphicsPresenter(device, parameters); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"DeviceWindowHandle\"))\n{\n    // fall back to offscreen presenter or re-init with a real window handle\n}","preventionTips":["Always set DeviceWindowHandle before GraphicsDevice/presenter creation","Use offscreen renderers for headless scenarios instead of nulling the handle","Track window lifetime and stop presenting/recreating after disposal","Assert handle.Handle != IntPtr.Zero in app startup"],"tags":["graphics","direct3d","swapchain","window-handle"],"backgroundTag":"null-argument","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}