fix: prevent TypeInitializationException crash#1808
Open
DineshSolanki wants to merge 1 commit into
Open
Conversation
…ructor The Window static constructor loaded a 14K-line ResourceDictionary (Theme.xaml) via GetResourceInternal, which could throw ArgumentOutOfRangeException from WPF internals when Application.Current was null or resources were not fully initialized. Once the static constructor failed, .NET cached the TypeInitializationException permanently, making all subsequent Window/MessageBox instantiations crash for the lifetime of the AppDomain. Changes: - ResourceHelper: use Lazy<ResourceDictionary> for thread-safe theme initialization; add Application.Current.TryFindResource fallback in GetResourceInternal when theme dictionary lookup fails or is null - Window: wrap static constructor in try-catch so a resource loading failure falls back to a null style instead of poisoning the type permanently Fixes HandyOrg#442
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
ResourceHelper.cs — 2 fixes:
_Thread-safe theme initialization: Replaced the non-thread-safe theme ??= GetStandaloneTheme() with Lazy. The old pattern had a race condition where concurrent callers could both trigger GetStandaloneTheme() simultaneously, corrupting WPF's internal ResourceDictionary state.
Fallback to Application.Current.TryFindResource: If the standalone Theme.xaml lookup fails (returns null or throws), GetResourceInternal now falls back to searching the live application resource tree. This handles the case where the theme is loaded via Application.Resources (the normal WPF pattern) rather than the standalone Theme.xaml path.
Window.cs — 1 fix:
Resilient static constructor: Wrapped the StyleProperty.OverrideMetadata call in try-catch. If resource loading fails for any reason, it falls back to a null style metadata instead of throwing. This prevents the fatal TypeInitializationException that .NET caches permanently — once that fires, every Window/MessageBox creation in the AppDomain is dead forever.
Fixes #442