Skip to content

Skip disposed controls instead of aborting ReapplyLocalizationsToAllComponents - #176

Merged
tombogle merged 6 commits into
masterfrom
issue-175-avoid-abort-on-disposed-control
Nov 20, 2026
Merged

tombogle merged 6 commits into
masterfrom
issue-175-avoid-abort-on-disposed-control

Conversation

@tombogle

@tombogle tombogle commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • ReapplyLocalizationsToAllComponents() walked ComponentCache with no check for whether a registered control had already been disposed and no per-item try/catch. A single disposed control threw ObjectDisposedException mid-enumeration, aborting the whole reapply pass and leaving every later-enumerated component un-relocalized for that language switch.
  • Because the stale entry was never removed from ComponentCache, the same abort happened again on every subsequent language switch for the rest of the process.
  • RefreshToolTips() already filters ComponentCache down to Control entries before its loop, so it shares the same vulnerable shape and gets the same fix, for the same reason (not for other component kinds, which it never touches).
  • Fix: both methods pre-check IsDisposed for Control and ToolStripItem (both actually expose that property) before touching the component, removing a confirmed-disposed entry outright. The remaining try/catch (ObjectDisposedException) is now a safety net for the rare case where a component is disposed in a race between the check and the work (e.g. an earlier component's localization disposes a later one on the UI thread), or where the exception comes from something else reached during that work (an owning ToolTip, or caller-supplied code behind an ILocalizableComponent) -- the catch re-verifies disposal before pruning, so a component that merely propagates someone else's ObjectDisposedException isn't misattributed as disposed and silently evicted while still alive. ColumnHeader and DataGridViewColumn have no IsDisposed property, and testing confirms they don't throw ObjectDisposedException on a disposed owner either (their owner back-reference goes null on disposal, so there's no reliable signal at all) -- they're left outside this mechanism, same as before this PR.
  • Pruning a cache entry now also removes it from the sibling LocalizableComponents dictionary, if present, so an evicted ILocalizableComponent isn't left referenced there indefinitely either.
  • ApplyLocalizationsToLocalizableComponent had no per-string try/catch: one disposed sub-item (locInfo.Component) owned by a still-live ILocalizableComponent container threw ObjectDisposedException and skipped every string after it in that container, on every subsequent switch too. Each string's ApplyLocalizationToString call is now independently guarded, so a disposed sub-item no longer blocks the container's other strings. If the container itself (not just the sub-item) can be confirmed disposed, the exception is rethrown instead of swallowed, so the outer catch gets a chance to prune the container's own cache entry rather than leaking it forever.
  • When a catch can't confirm the component is actually disposed (so it stays cached and the same exception will likely recur on the next switch), it's now traced via Trace.WriteLine instead of being silently swallowed -- and rather than Debug.Fail, which a standalone repro showed terminates the process outright on net8.0-windows (Debug.WriteLine was tried first, then switched to Trace.WriteLine since the former is compiled out of Release builds, including the published NuGet package).

Fixes #175.

Test plan

  • Regression test (adapted from the repro in the issue) asserting ReapplyLocalizationsToAllComponents() no longer throws when a registered control has been disposed, that a later-registered live control still gets relocalized, and that the disposed control's entry is pruned from ComponentCache.
  • Regression test for a disposed, cached ToolStripItem (parented to a ToolStrip, since IsDisposed only reflects reality once an item belongs to one), confirming it's pruned via its own pre-check.
  • Regression test proving a component that throws ObjectDisposedException for a reason unrelated to its own disposal is not evicted from the cache.
  • Regression test for RefreshToolTips() with a disposed registered control.
  • Regression test proving a disposed Control/ILocalizableComponent is pruned from both ComponentCache and LocalizableComponents, not just the former.
  • Regression test proving a disposed sub-item under one id doesn't block localizing a live sub-item under another id on the same ILocalizableComponent container.
  • dotnet test passes for all three target frameworks (net48, net462, net8.0-windows) with no regressions.

🤖 Generated with Claude Code


Devin Review


This change is Reviewable

…izationsToAllComponents

A disposed control left in ComponentCache throws ObjectDisposedException
mid-enumeration, aborting the rest of the reapply pass and recurring on
every subsequent language switch since the stale entry is never removed.
ReapplyLocalizationsToAllComponents and RefreshToolTips now skip/remove
a disposed entry instead of letting it propagate. Fixes #175.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Devin Review

@github-actions

github-actions Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Test Results

    7 files  ±  0  154 suites  ±0   32s ⏱️ +4s
238 tests +  6  233 ✔️ +  6    5 💤 ±0  0 ❌ ±0 
914 runs  +18  899 ✔️ +18  15 💤 ±0  0 ❌ ±0 

Results for commit c9580c2. ± Comparison against base commit 715fc21.

♻️ This comment has been updated with latest results.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@tombogle tombogle self-assigned this Nov 17, 2026
imnasnainaec

This comment was marked as resolved.

…trol fix

Extends the IsDisposed pre-check to ToolStripItem (it has one, despite
the prior comment's claim), shares the check/prune/catch shape between
ReapplyLocalizationsToAllComponents and RefreshToolTips, and re-checks
disposal before evicting a component in the catch block so a component
that throws ObjectDisposedException for an unrelated reason (a race, or
code reached through an ILocalizableComponent) isn't misattributed as
disposed and silently dropped from the cache forever. Also prunes the
LocalizableComponents dictionary alongside ComponentCache.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@tombogle

Copy link
Copy Markdown
Contributor Author

Flagging as out-of-scope for this PR (noting it here so it isn't re-surfaced in a later review):

ReapplyLocalizationsToAllObjectsInAllManagers (and the single-manager ReapplyLocalizationsToAllObjects) in LocalizationManagerInternalWinforms.cs have no try/catch around the per-manager ReapplyLocalizationsToAllComponents() call. If a disposed component throws something other than ObjectDisposedException (or an exception type the per-component catch can't anticipate), it still propagates out and aborts the foreach over LoadedManagers.Values, reproducing issue #175's original severity one layer up, across managers instead of across components.

This is pre-existing code, not touched by this PR, so it's being called out separately rather than addressed here.

@tombogle
tombogle marked this pull request as draft November 19, 2026 13:47
…sposed

The generic name and its Action parameter overstated how broadly this
helper is used: both call sites only ever pass a localization-related
step (ApplyLocalization, ApplyLocalizedToolTipToControl), which the
accompanying comment already assumed ("localizing", "unlocalized
forever"). Name it for what it actually does.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@tombogle
tombogle marked this pull request as ready for review November 19, 2026 16:58
imnasnainaec

This comment was marked as resolved.

- ApplyLocalizationsToLocalizableComponent now catches
  ObjectDisposedException per string, so a disposed sub-item owned by a
  still-live ILocalizableComponent container no longer blocks localizing
  the container's other strings.
- When RunLocalizationStepUnlessDisposed's catch can't confirm the
  component is actually disposed (so it stays cached), trace it via
  Debug.WriteLine.
- Added regression tests: LocalizableComponents is pruned alongside
  ComponentCache (not just the latter); a disposed sub-item under one
  id doesn't block localizing a live sub-item under another id on the
  same container.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@tombogle

Copy link
Copy Markdown
Contributor Author

Thanks for the thorough follow-up review. Responses to each point (pushed in bea7735):

1. ILocalizableComponent with several strings — Fixed: ApplyLocalizationsToLocalizableComponent now wraps each ApplyLocalizationToString call in its own try/catch (ObjectDisposedException), so a disposed sub-item under one id no longer blocks the container's other strings. Added a regression test with two sub-ids, one of which always throws, asserting the other is still localized.

2. Disposed ToolStripItem with no owner — Confirmed via a standalone repro: Dispose() on a ToolStripItem with no Owner is effectively a near no-op (it doesn't even flip IsDisposed), so there's nothing actually released to make it a zombie object. No code change — agreed this is harmless, not a leak of anything real, and "still works correctly if re-added to a parent" holds.

3. Test gaps — Added a test proving LocalizableComponents is pruned alongside ComponentCache, not just the latter (reachable via the existing IsDisposed pre-check, no contrived setup needed), plus the #1 test above. Deliberately did not add a test that reaches the catch in RunLocalizationStepUnlessDisposed itself (a control that disposes itself mid-OnTextChanged and then touches its own handle) — contrived enough to not be worth it for what's "icing on the cake" cleanup once a component actually is disposed.

4. Disposed event — Agreed this is the more thorough fix, but we're deliberately keeping this PR narrow/lower-risk. Filed #177 to track it as a follow-on, including the note that it still wouldn't help the case where the owning grid/list-view is disposed without disposing its columns/headers.

5. Silent swallow — Implemented, but with Debug.WriteLine rather than Debug.Fail: tested Debug.Fail directly and on net8.0-windows it terminates the entire process ("Process terminated. Assertion failed."), not just a blocking dialog. That's far worse than the bug being fixed (and would have broken our own regression test that deliberately exercises this branch). Debug.WriteLine keeps the cause visible in DEBUG builds with no such risk. Added the same trace to the ApplyLocalizationsToLocalizableComponent per-string catch from #1 for consistency.

6. Controls mid-dispose — Agreed harmless, no change.

7. Other exception types — Agreed, out of scope here. Filed #178 to track a possible pluggable error-handling delegate as a future API addition; feels like YAGNI until a concrete case shows up.

@tombogle
tombogle enabled auto-merge (squash) November 20, 2026 20:08

@imnasnainaec imnasnainaec left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drafted by Claude Opus 5.5; partly human-reviewed.

Thanks for the updates. The per-string guard and the reasoning about Debug.Fail both look right, and #177 and #178 cover the rest.

⛏️ The PR description and test plan don't mention the per-string guard in ApplyLocalizationsToLocalizableComponent, the trace in the branch where disposal can't be confirmed, or the two new tests. Consider updating them.

Comment thread CHANGELOG.md Resolved
…a Trace not Debug

- ApplyLocalizationsToLocalizableComponent's per-string catch now
  rethrows if locComponent itself (not just the sub-item it owns) can
  be confirmed disposed, so the outer RunLocalizationStepUnlessDisposed
  catch gets a chance to prune its ComponentCache entry. Previously a
  disposed container's entry could never be pruned, since the pre-check
  can't detect disposal for an ILocalizableComponent either.
- Swapped both Debug.WriteLine traces for Trace.WriteLine: Debug.* is
  compiled out of Release builds, including the published NuGet
  package, so consuming apps could never see it; Trace.* stays in by
  default in SDK-style projects.
- Updated the CHANGELOG entry and PR description to cover the
  per-string guard and the trace, not just the disposed-control fix.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

@imnasnainaec imnasnainaec left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@imnasnainaec reviewed 3 files and all commit messages, and resolved 4 discussions.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on tombogle).

@tombogle
tombogle merged commit b504934 into master Nov 20, 2026
5 of 6 checks passed
@tombogle
tombogle deleted the issue-175-avoid-abort-on-disposed-control branch November 20, 2026 21:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ReapplyLocalizationsToAllComponents aborts (and keeps aborting) on a disposed registered control

2 participants