{"record":{"id":"a0e155fd3e57f0f8","repo":"dotnet/wpf","slug":"sr-notininitialization-glyphtypeface","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/GlyphTypeface.cs","lineNumber":1667,"sourceCode":"                // Cannot initialize a GlyphRun this is completely initialized.\n                throw new InvalidOperationException(SR.OnlyOneInitialization);\n            }\n\n            if (_initializationState == InitializationState.IsInitializing)\n            {\n                // Cannot initialize a GlyphRun this already being initialized.\n                throw new InvalidOperationException(SR.InInitialization);\n            }\n\n            _initializationState = InitializationState.IsInitializing;\n        }\n\n        void ISupportInitialize.EndInit()\n        {\n            if (_initializationState != InitializationState.IsInitializing)\n            {\n                // Cannot EndInit a GlyphRun that is not being initialized.\n                throw new InvalidOperationException(SR.NotInInitialization);\n            }\n\n            Initialize(_originalUri, _styleSimulations);\n        }\n\n        private void CheckInitialized()\n        {\n            if (_initializationState != InitializationState.IsInitialized)\n            {\n                throw new InvalidOperationException(SR.InitializationIncomplete);\n            }\n        }\n\n        private void CheckInitializing()\n        {\n            if (_initializationState != InitializationState.IsInitializing)\n            {\n                throw new InvalidOperationException(SR.NotInInitialization);","sourceCodeStart":1649,"sourceCodeEnd":1685,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/GlyphTypeface.cs#L1649-L1685","documentation":"GlyphTypeface's ISupportInitialize.EndInit throws SR.NotInInitialization when EndInit is called while _initializationState is not IsInitializing, i.e. when no BeginInit cycle is open. EndInit is only valid as the closing half of a BeginInit/EndInit pair, otherwise it throws InvalidOperationException. The same guard backs private CheckInitializing (error 1154).","triggerScenarios":"Calling ISupportInitialize.EndInit() on a fresh GlyphTypeface before any BeginInit; calling EndInit twice; calling EndInit after initialization already completed (state IsInitialized).","commonSituations":"Cleanup/finally blocks that call EndInit unconditionally even when BeginInit failed; generated designer code with mismatched Begin/EndInit emission; copy-pasted two-phase initialization code where BeginInit was removed or commented out.","solutions":["Call BeginInit before EndInit so the pair is balanced.","Only call EndInit in a finally block when the matching BeginInit actually succeeded (use a boolean flag).","Remove redundant EndInit calls; a completed GlyphTypeface needs no further EndInit."],"exampleFix":"// before\ntry\n{\n    ((ISupportInitialize)face).BeginInit();\n}\nfinally\n{\n    ((ISupportInitialize)face).EndInit(); // throws if BeginInit threw\n}\n\n// after\nvar inited = false;\ntry\n{\n    ((ISupportInitialize)face).BeginInit();\n    inited = true;\n}\nfinally\n{\n    if (inited) ((ISupportInitialize)face).EndInit();\n}","handlingStrategy":"validation","validationCode":"// Track your own cycle state so EndInit is only called after a successful BeginInit\nbool beganInit = false;\n((ISupportInitialize)face).BeginInit(); beganInit = true;\nif (beganInit) ((ISupportInitialize)face).EndInit();","typeGuard":"static bool CanEndInit(bool beginSucceeded, bool endAlreadyCalled) => beginSucceeded && !endAlreadyCalled;","tryCatchPattern":"try { ((ISupportInitialize)face).EndInit(); }\ncatch (InvalidOperationException) { /* no open initialization cycle; ignore or fail fast in tests */ }","preventionTips":["Always pair EndInit with a BeginInit that actually succeeded; use a boolean flag in finally blocks.","Never call EndInit more than once per cycle.","Review cleanup code paths for unconditional EndInit calls."],"tags":["wpf","glyphrun","ispupportinitialize","initialization-state","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"}