{"record":{"id":"3fee90a82b69905c","repo":"dotnet/wpf","slug":"sr-ininitialization","errorCode":null,"errorMessage":"SR.InInitialization","messagePattern":"SR\\.InInitialization","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/GlyphRun.cs","lineNumber":2297,"sourceCode":"            private int _count;\n        }\n\n        #endregion Hit testing\n\n        #region ISupportInitialize interface for Xaml serialization\n\n        void ISupportInitialize.BeginInit()\n        {\n            if (IsInitialized)\n            {\n                // 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(","sourceCodeStart":2279,"sourceCodeEnd":2315,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/GlyphRun.cs#L2279-L2315","documentation":"BeginInit() throws InvalidOperationException(SR.InInitialization) when BeginInit is called while a previous initialization batch is still open (IsInitializing is true). Initialization batches must not be nested: exactly one BeginInit/EndInit cycle at a time.","triggerScenarios":"Calling BeginInit() twice without an intervening EndInit() — e.g. nested setup code, an exception skipping EndInit, or two code paths both driving ISupportInitialize on the same GlyphRun.","commonSituations":"Exception in the middle of an init block leaves IsInitializing stuck true; a retry then throws. Also occurs when XAML loader and custom code both call BeginInit on the same element.","solutions":["Pair every BeginInit with EndInit in a try/finally so an exception cannot leave the GlyphRun stuck in the initializing state.","Check glyphRun.IsInitializing before calling BeginInit() and skip or end the prior batch first.","Ensure only one code path drives ISupportInitialize for a given GlyphRun instance."],"exampleFix":"// before\nglyphRun.BeginInit();\nInitPart1(glyphRun);\nglyphRun.BeginInit(); // nested: throws\n// after\nglyphRun.BeginInit();\ntry { InitPart1(glyphRun); InitPart2(glyphRun); }\nfinally { glyphRun.EndInit(); }","handlingStrategy":"try-catch","validationCode":"if (!glyphRun.IsInitializing) glyphRun.BeginInit();","typeGuard":"bool CanBeginInit(GlyphRun g) => !g.IsInitializing;","tryCatchPattern":"try { glyphRun.BeginInit(); /* set props */ glyphRun.EndInit(); }\nfinally { if (glyphRun.IsInitializing) glyphRun.EndInit(); }","preventionTips":["Always pair BeginInit/EndInit inside try/finally.","Never call BeginInit twice on the same instance without EndInit.","Ensure a single owner drives the initialization lifecycle."],"tags":["wpf","glyphrun","initialization","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-22T01:17:13.364Z"}