stride3d/stride · critical · InvalidOperationException

Unable to find a GraphicsDevice instance

Error message

Unable to find a GraphicsDevice instance

What it means

After locating IGraphicsDeviceService in the GameBase constructor, it checks that the service actually exposes a non-null GraphicsDevice. A registered service with a null device means no real rendering device was created, so the constructor throws this InvalidOperationException.

Solutions

  1. Ensure the IGraphicsDeviceService creates/returns a valid GraphicsDevice before the Game constructor runs
  2. Verify GPU drivers and that the platform supports device creation
  3. In tests, mock a non-null GraphicsDevice or use Stride's headless abstractions

Example fix

// before
public class FakeService : IGraphicsDeviceService { public GraphicsDevice GraphicsDevice => null; }
// after
public class FakeService : IGraphicsDeviceService { public GraphicsDevice GraphicsDevice { get; } = CreateMockDevice(); }
Defensive patterns

Strategy: validation

Validate before calling

if (services.GetService<IGraphicsDeviceService>()?.GraphicsDevice == null) throw new InvalidOperationException("GraphicsDevice not ready");

Type guard

bool HasDevice(IGraphicsDeviceService s) => s?.GraphicsDevice != null;

Try / catch

try { var game = new MyGame(); } catch (InvalidOperationException ex) when (ex.Message.Contains("GraphicsDevice instance")) { /* fix device creation, then retry */ }

Prevention

When it happens

Trigger: A custom or stub IGraphicsDeviceService registered in Services whose GraphicsDevice property is null; device creation failing silently before Game construction; uninitialized custom device implementations.

Common situations: Test doubles returning null GraphicsDevice; custom device factories failing on machines without supported GPU/drivers; creating the service before the device is created.

Understand the failure class

Background: "not installed", "pip install", "required for": how missing-dependency errors surface across open-source libraries — this error's family across 34 libraries.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/4444ff05cbc4f732. Report an issue: GitHub.

Appendix: source

Thrown at sources/engine/Stride.Games/GameBase.cs:966

        private void GamePlatform_Exiting(object sender, EventArgs e)
        {
            OnExiting(this, EventArgs.Empty);
        }

        private void SetupGraphicsDeviceEvents()
        {
            // Find the IGraphicsDeviceSerive.
            graphicsDeviceService = Services.GetService<IGraphicsDeviceService>();

            // If there is no graphics device service, don't go further as the whole Game would not work
            if (graphicsDeviceService == null)
            {
                throw new InvalidOperationException("Unable to create a IGraphicsDeviceService");
            }

            if (graphicsDeviceService.GraphicsDevice == null)
            {
                throw new InvalidOperationException("Unable to find a GraphicsDevice instance");
            }

            resumeManager = new ResumeManager(Services);

            GraphicsDevice = graphicsDeviceService.GraphicsDevice;
            graphicsDeviceService.DeviceCreated += GraphicsDeviceService_DeviceCreated;
            graphicsDeviceService.DeviceResetting += GraphicsDeviceService_DeviceResetting;
            graphicsDeviceService.DeviceReset += GraphicsDeviceService_DeviceReset;
            graphicsDeviceService.DeviceDisposing += GraphicsDeviceService_DeviceDisposing;
        }

        private void DisposeGraphicsDeviceEvents()
        {
            if (graphicsDeviceService != null)
            {
                graphicsDeviceService.DeviceCreated -= GraphicsDeviceService_DeviceCreated;
                graphicsDeviceService.DeviceResetting -= GraphicsDeviceService_DeviceResetting;
                graphicsDeviceService.DeviceReset -= GraphicsDeviceService_DeviceReset;

View on GitHub (pinned to 96fad776d2)