{"record":{"id":"7b8b4b107d54d8e5","repo":"slint-ui/slint","slug":"context-shouldn-t-have-been-set-before","errorCode":null,"errorMessage":"context shouldn't have been set before","messagePattern":"context shouldn't have been set before","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/core/window.rs","lineNumber":2509,"sourceCode":"        self.ctx\n            .get_or_init(|| crate::context::GLOBAL_CONTEXT.with(|ctx| ctx.get().unwrap().clone()))\n    }\n\n    /// Like [`Self::context`], but returns `None` instead of panicking when no context is\n    /// available yet.\n    pub fn try_context(&self) -> Option<&crate::SlintContext> {\n        if self.ctx.get().is_none()\n            && let Some(ctx) = crate::context::GLOBAL_CONTEXT.with(|ctx| ctx.get().cloned())\n        {\n            let _ = self.ctx.set(ctx);\n        }\n        self.ctx.get()\n    }\n\n    /// Set the SlintContext.\n    /// This needs to be called once before any other functions that would use the context.\n    pub fn set_context(&self, ctx: crate::SlintContext) {\n        self.ctx.set(ctx).map_err(|_| ()).expect(\"context shouldn't have been set before\")\n    }\n}\n\n/// Internal alias for `Rc<dyn WindowAdapter>`.\npub type WindowAdapterRc = Rc<dyn WindowAdapter>;\n\n/// Resolve the [`crate::SlintContext`] associated with a component root by\n/// asking it for (or creating) its window adapter and reading the context off\n/// the resulting window. Returns `None` only when no adapter can be produced.\npub fn context_for_root(root: &ItemTreeRc) -> Option<crate::SlintContext> {\n    let comp_ref_pin = vtable::VRc::borrow_pin(root);\n    let mut adapter = None;\n    comp_ref_pin.as_ref().window_adapter(true, &mut adapter);\n    adapter.map(|a| WindowInner::from_pub(a.window()).context().clone())\n}\n\n/// Runtime entry point for `BuiltinFunction::AccentColor`. Returns the accent color\n/// from the component's [`crate::SlintContext`] reached via its window adapter, or","sourceCodeStart":2491,"sourceCodeEnd":2527,"githubUrl":"https://github.com/slint-ui/slint/blob/3a7e700487dc74d404cad0726378053ab763d4e0/internal/core/window.rs#L2491-L2527","documentation":"`WindowInner::set_context` stores the `SlintContext` in a `OnceCell`-like cell and panics if the context was already set. The context must be initialized exactly once before any other window function uses it. A double initialization means two runtime instances are driving the same window machinery.","triggerScenarios":"Calling `set_context` twice on the same window, or creating/initializing a component after the context was already installed (e.g. two `slint::Platform`/runtime initializations in the same process).","commonSituations":"Embedding Slint twice in one process (two independent Slint init calls); mixing static-library copies of slint; custom backend code calling set_context manually; test harnesses that re-initialize the platform per test.","solutions":["Initialize the Slint context/platform only once per process (guard with std::sync::Once)","Ensure only one copy/version of the slint crate is linked (check `cargo tree -d`)","If writing backend code, check `self.ctx.get().is_some()` before calling set_context","Do not call set_context manually unless implementing a custom backend/platform"],"exampleFix":"// before\nset_context(ctx);\nset_context(ctx); // panics\n\n// after\nstatic INIT: std::sync::Once = std::sync::Once::new();\nINIT.call_once(|| set_context(ctx));","handlingStrategy":"validation","validationCode":"static INIT: std::sync::Once = std::sync::Once::new();\nINIT.call_once(|| slint::platform::set_platform(...).expect(\"platform init\"));","typeGuard":"fn context_is_unset(w: &slint::WindowInner) -> bool { w.ctx.get().is_none() }","tryCatchPattern":null,"preventionTips":["Initialize the Slint platform/context exactly once with std::sync::Once","Check cargo tree -d for duplicate slint crate copies","Do not call internal set_context outside custom backend code"],"tags":["gui","initialization","window","panic"],"backgroundTag":"invalid-state-transition","analyzedSha":"3a7e700487dc74d404cad0726378053ab763d4e0","analyzedAt":"2026-09-16T01:37:20.251Z","contentChangedAt":"2026-09-16T01:37:20.251Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}