{"record":{"id":"262eeea7f0cd2d70","repo":"dotnet/wpf","slug":"sr-onlyoneinitialization-glyphtypeface","errorCode":null,"errorMessage":"SR.OnlyOneInitialization","messagePattern":"SR\\.OnlyOneInitialization","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/GlyphTypeface.cs","lineNumber":1650,"sourceCode":"            {\n                return localizedStrings;\n            }\n            else\n            {\n                return new MS.Internal.Text.TextInterface.LocalizedStrings();\n            }\n        }\n \n        #endregion Private Methods\n\n        #region ISupportInitialize interface\n\n        void ISupportInitialize.BeginInit()\n        {\n            if (_initializationState == InitializationState.IsInitialized)\n            {\n                // 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            }","sourceCodeStart":1632,"sourceCodeEnd":1668,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/GlyphTypeface.cs#L1632-L1668","documentation":"GlyphTypeface implements ISupportInitialize. BeginInit throws SR.OnlyOneInitialization when the GlyphTypeface is already fully initialized (_initializationState == InitializationState.IsInitialized), because its two-phase initialize-once lifecycle forbids re-initialization. This is an InvalidOperationException raised from the public ISupportInitialize.BeginInit method in GlyphTypeface.cs.","triggerScenarios":"Calling ISupportInitialize.BeginInit() (or ((ISupportInitialize)glyphTypeface).BeginInit()) on a GlyphTypeface instance that has already completed a full BeginInit/EndInit cycle, i.e. calling BeginInit twice without constructing a new GlyphTypeface in between.","commonSituations":"Reusing a cached GlyphTypeface for a second font load; component designer/serializer code that calls BeginInit on an existing instance; shared helper methods that defensively call BeginInit on objects that may already be initialized; re-running XAML load logic that re-initializes font resources.","solutions":["Do not call BeginInit again on the same GlyphTypeface; create a new GlyphTypeface for the new font/URI.","Track your own initialization flag and skip BeginInit when the instance is already initialized.","If the font face must change, dispose of/recreate the GlyphTypeface rather than re-initializing it."],"exampleFix":"// before\nvar face = new GlyphTypeface(fontUri);\n((ISupportInitialize)face).BeginInit(); // throws if already initialized\n\n// after\nif (((ISupportInitialize)face).IsInitialized) // or track your own flag\n{\n    face = new GlyphTypeface(fontUri); // create a fresh instance instead of re-initializing\n}\n((ISupportInitialize)face).BeginInit();","handlingStrategy":"type-guard","validationCode":"bool isInitialized = ((ISupportInitialize)face).IsInitialized;\nif (isInitialized) { face = new GlyphTypeface(fontUri); }","typeGuard":"static bool CanBeginInit(GlyphTypeface face) => !((ISupportInitialize)face).IsInitialized;","tryCatchPattern":"try { ((ISupportInitialize)face).BeginInit(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"initializ\")) { /* instance already initialized; reuse as-is or recreate */ }","preventionTips":["Treat GlyphTypeface as initialize-once: one BeginInit/EndInit cycle per instance.","Centralize GlyphTypeface creation in a factory so ad-hoc BeginInit calls never occur twice.","Track the lifecycle state in your own field if the instance is shared across call sites."],"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-21T21:30:21.729Z"}