{"record":{"id":"6bfeeed848f82f0c","repo":"stride3d/stride","slug":"cannot-use-a-different-context-between-load-and-draw","errorCode":null,"errorMessage":"Cannot use a different context between Load and Draw","messagePattern":"Cannot use a different context between Load and Draw","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/engine/Stride.Rendering/Rendering/RendererCoreBase.cs","lineNumber":251,"sourceCode":"            {\n                context.QueryManager.BeginProfile(Color.Green, GPUProfilingKey);\n            }\n\n            PreDrawCore(context);\n\n            // Allow scoped allocation RenderTargets\n            isInDrawCore = true;\n        }\n\n        protected void EnsureContext(RenderContext context)\n        {\n            if (Context == null)\n            {\n                Initialize(context);\n            }\n            else if (Context != context)\n            {\n                throw new InvalidOperationException(\"Cannot use a different context between Load and Draw\");\n            }\n        }\n\n        protected void PostDrawCoreInternal(RenderDrawContext context)\n        {\n            isInDrawCore = false;\n\n            // Release scoped RenderTargets\n            ReleaseAllScopedResources();\n\n            PostDrawCore(context);\n\n            if (GPUProfilingKey.Name != null && Profiling)\n            {\n                context.QueryManager.EndProfile(GPUProfilingKey);\n            }\n        }\n    }","sourceCodeStart":233,"sourceCodeEnd":269,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/engine/Stride.Rendering/Rendering/RendererCoreBase.cs#L233-L269","documentation":"RendererCoreBase caches the RenderContext it was initialized/loaded with and requires the same context for every subsequent Draw. EnsureContext compares the incoming context with the stored one; a mismatch means the renderer's state (services, graphics device, resources) belongs to a different pipeline instance, so it throws InvalidOperationException rather than silently drawing with the wrong context.","triggerScenarios":"Calling PreDrawCoreInternal/Draw with a RenderDrawContext whose RenderContext differs from the one used during Initialize/Load — e.g. rendering the same feature through two GraphicsCompositors/render systems, sharing a RenderFeature or Material across two scenes/games, or drawing on a different RenderContext than the one the renderer loaded with.","commonSituations":"Reusing render stages, features or renderers across multiple SceneRenderers; multi-window or offscreen rendering where each target has its own RenderContext; running rendering logic on another Game instance's context.","solutions":["Ensure each RenderFeature/renderer is used with exactly one RenderContext; create a separate instance per context","If sharing is intended, reset the renderer (Destroy/Dispose and recreate) when switching contexts","Check for accidentally cached/shared RenderFeature instances (static fields, singleton services) between scenes or games","Route all draws through the compositor that owns the renderer instead of calling Draw directly with a foreign context"],"exampleFix":"// before\nsharedFeature.Draw(drawContextOfOtherScene); // InvalidOperationException\n// after\nvar feature = new MyRenderFeature();\nfeature.Initialize(sceneContext);\nfeature.Draw(sceneDrawContext);","handlingStrategy":"validation","validationCode":"if (renderer.Context != null && renderer.Context != context.RenderContext)\n    throw new InvalidOperationException(\"Renderer bound to a different RenderContext.\");","typeGuard":null,"tryCatchPattern":"try\n{\n    feature.Draw(drawContext);\n}\ncatch (InvalidOperationException) when (ex.Message.Contains(\"different context\"))\n{\n    // recreate feature bound to this context\n    feature.Dispose();\n    feature = CreateFeatureFor(context);\n}","preventionTips":["Never share RenderFeature/renderer instances across scenes or games","Create one renderer per RenderContext","Avoid static/singleton renderers"],"tags":["rendering","lifecycle","context"],"backgroundTag":"unsupported-operation","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}