fix(i18n): stop leaking a ResourceDictionary on every language switch - #165
Open
divya0795 wants to merge 1 commit into
Open
fix(i18n): stop leaking a ResourceDictionary on every language switch#165divya0795 wants to merge 1 commit into
divya0795 wants to merge 1 commit into
Conversation
Contributor
Author
|
Compile verification: dispatched this repo's own Result: success in ~1.6 min https://github.com/divya0795/Wand-Enhancer/actions/runs/30205234754 That satisfies "your code compiles without errors" from CONTRIBUTING.md. The manual-testing-against-WeMod step remains outstanding on my side, as flagged in the PR description. |
SetLanguage builds the replacement locale dictionary with "new ResourceDictionary()" and never assigns a Source, but the lookup that finds the dictionary to replace requires "d.Source != null". Only the first switch matches, because that one still targets the Source-bearing Locale/lang.en-US.xaml merged by App.xaml. From then on the installed dictionary has no Source, the lookup returns null, and control falls to the else branch, which appends without removing. Switching languages N times therefore leaves N fully parsed locale dictionaries merged into Application.Current.Resources. Displayed text stays correct because the last merged dictionary wins, so the symptom is an unbounded memory leak rather than wrong strings. Track the installed dictionary in a static field and prefer it when looking up the one to replace, falling back to the Source-based predicate for the first call. The index is checked before use so a dictionary that is no longer merged appends instead of throwing.
divya0795
force-pushed
the
fix/localization-dictionary-leak
branch
from
July 26, 2026 14:32
f593eea to
9bbc96d
Compare
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.
Fixes #164
Problem
LocalizationManager.SetLanguagereplaces the merged localeResourceDictionarycorrectly once, then appends on every subsequent switch without removing the previous one.The replacement dictionary is built with
new ResourceDictionary()and has noSource(:85), but the lookup for the dictionary to replace requiresd.Source != null(:108-109). The first switch matches App.xaml'sLocale/lang.en-US.xaml; after that the installed dictionary has noSource, the lookup returnsnull, and control reaches theelsebranch that calls.Add(...).Switching language N times leaves N parsed locale dictionaries merged for the process lifetime. Text stays correct — the last merged dictionary wins — so the symptom is an unbounded memory leak, not wrong strings.
Change
Track the installed dictionary in
_currentLocaleDictionaryand prefer it when locating the one to replace, keeping the existingSource-based predicate as the fallback for the first call.The index is now resolved before use and the
elsebranch is taken when it is-1, so a tracked dictionary that is no longer merged appends instead of throwing onInsert(-1, ...). Behaviour is otherwise unchanged: same position inMergedDictionaries, same English-base-plus-overlay construction, same settings save.One file changed,
+17 / -5.Verification
master@643c8f8..github/workflows/build.yml(fullbuild.ps1→ MSBuild,windows-latest) on my fork — I have no local Visual Studio/MSBuild environment. Run linked in a comment below.Two limits I want to be explicit about rather than imply otherwise:
MergedDictionariesbehaviour needs a runningApplication, so there is no unit test accompanying this.