{"record":{"id":"7515ac057a1f843f","repo":"dotnet/wpf","slug":"sr-notininitialization","errorCode":null,"errorMessage":"SR.NotInInitialization","messagePattern":"SR\\.NotInInitialization","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/GlyphRun.cs","lineNumber":2308,"sourceCode":"                // Cannot initialize a GlyphRun that is completely initialized.\n                throw new InvalidOperationException(SR.OnlyOneInitialization);\n            }\n\n            if (IsInitializing)\n            {\n                // Cannot initialize a GlyphRun that is already being initialized.\n                throw new InvalidOperationException(SR.InInitialization);\n            }\n\n            IsInitializing = true;\n        }\n\n        void ISupportInitialize.EndInit()\n        {\n            if (!IsInitializing)\n            {\n                // Cannot EndInit a GlyphRun that is not being initialized.\n                throw new InvalidOperationException(SR.NotInInitialization);\n            }\n\n            //\n            // Fully initilize the GlyphRun. The method will check for consistency\n            // between all the properties.\n            //\n            Initialize(\n                _glyphTypeface,\n                _bidiLevel,\n                (_flags & GlyphRunFlags.IsSideways) != 0,\n                _renderingEmSize,\n                _pixelsPerDip,\n                _glyphIndices,\n                _baselineOrigin,\n                // In case the layout mode is not Ideal then we cannot use ThousandthOfEmReal* since ThousandthOfEmReal* internally stores doubles as integers and hence there is some lost percision\n                // that can result in glyphs that were pixel aligned be not so. This is not important for ideal layout but is of great importance for compatible with layout.\n                (_advanceWidths == null ? null : ((_textFormattingMode != TextFormattingMode.Ideal) ? (IList<double>)(new List<double>()) : (IList<double>)(new ThousandthOfEmRealDoubles(_renderingEmSize, _advanceWidths)))),\n                (_glyphOffsets == null ? null : ((_textFormattingMode != TextFormattingMode.Ideal) ? (IList<Point>)(new List<Point>()) : (IList<Point>)(new ThousandthOfEmRealPoints(_renderingEmSize, _glyphOffsets)))),","sourceCodeStart":2290,"sourceCodeEnd":2326,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/GlyphRun.cs#L2290-L2326","documentation":"GlyphRun implements ISupportInitialize so it can be built in two phases: BeginInit sets up the object, EndInit commits it. This InvalidOperationException is thrown by EndInit when the GlyphRun was never put into initializing state (BeginInit was not called, or EndInit was already called once). The library throws it because calling EndInit outside a BeginInit/EndInit pair is a programming error, not a runtime condition.","triggerScenarios":"Calling ((ISupportInitialize)glyphRun).EndInit() without a preceding BeginInit(), calling EndInit twice for one BeginInit, or calling EndInit on a GlyphRun constructed normally via its constructor without ever using ISupportInitialize at all.","commonSituations":"XAML/baml deserialization code paths that end initialization out of order, hand-rolled object initializers that mirror the XAML pattern but forget BeginInit, refactored code that removed a BeginInit call while keeping EndInit.","solutions":["Ensure BeginInit() is called on the GlyphRun before EndInit(), as a matched pair.","Guard the EndInit call with a check of the initialization state or a boolean flag you control.","If you do not need deferred initialization, construct GlyphRun with its full constructor and drop the ISupportInitialize calls entirely."],"exampleFix":"// before\nvar glyphRun = new GlyphRun(...);\n((ISupportInitialize)glyphRun).EndInit(); // throws: never began init\n\n// after\nvar glyphRun = new GlyphRun(...);\n((ISupportInitialize)glyphRun).BeginInit();\n// ... set properties ...\n((ISupportInitialize)glyphRun).EndInit();","handlingStrategy":"validation","validationCode":"static bool IsGlyphRunInitializing(GlyphRun run) =>\n    run is ISupportInitialize && GlyphRunStateTracker.IsInitializing(run); // track your own BeginInit/EndInit pairing\n// simplest practical guard: only call EndInit inside a scope where you called BeginInit\nusing var scope = GlyphInitScope.Begin(glyphRun); // helper that pairs BeginInit/EndInit","typeGuard":"static bool CanEndInit(ISupportInitialize obj, bool beginInitCalled) => beginInitCalled;","tryCatchPattern":"try { ((ISupportInitialize)glyphRun).EndInit(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"initialization\")) { /* recreate GlyphRun via constructor instead */ }","preventionTips":["Always pair BeginInit/EndInit in the same method or a dedicated scope/disposable so they cannot drift apart.","Prefer the full GlyphRun constructor over ISupportInitialize unless you need the XAML loader pattern.","Never call EndInit more than once per BeginInit; track the state with a boolean if the code paths are complex."],"tags":["wpf","glyphrun","initialization","invalid-state-transition","invalid-operation"],"backgroundTag":"invalid-state-transition","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}