Repository navigation
Skip disposed controls instead of aborting ReapplyLocalizationsToAllComponents - #176
Conversation
…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>
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
…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>
|
Flagging as out-of-scope for this PR (noting it here so it isn't re-surfaced in a later review):
This is pre-existing code, not touched by this PR, so it's being called out separately rather than addressed here. |
…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>
- 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>
|
Thanks for the thorough follow-up review. Responses to each point (pushed in bea7735): 1. 2. Disposed 3. Test gaps — Added a test proving 4. 5. Silent swallow — Implemented, but with 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. |
imnasnainaec
left a comment
There was a problem hiding this comment.
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.
…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
left a comment
There was a problem hiding this comment.
@imnasnainaec reviewed 3 files and all commit messages, and resolved 4 discussions.
Reviewable status:complete! all files reviewed, all discussions resolved (waiting on tombogle).
Summary
ReapplyLocalizationsToAllComponents()walkedComponentCachewith no check for whether a registered control had already been disposed and no per-itemtry/catch. A single disposed control threwObjectDisposedExceptionmid-enumeration, aborting the whole reapply pass and leaving every later-enumerated component un-relocalized for that language switch.ComponentCache, the same abort happened again on every subsequent language switch for the rest of the process.RefreshToolTips()already filtersComponentCachedown toControlentries 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).IsDisposedforControlandToolStripItem(both actually expose that property) before touching the component, removing a confirmed-disposed entry outright. The remainingtry/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 owningToolTip, or caller-supplied code behind anILocalizableComponent) -- the catch re-verifies disposal before pruning, so a component that merely propagates someone else'sObjectDisposedExceptionisn't misattributed as disposed and silently evicted while still alive.ColumnHeaderandDataGridViewColumnhave noIsDisposedproperty, and testing confirms they don't throwObjectDisposedExceptionon 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.LocalizableComponentsdictionary, if present, so an evictedILocalizableComponentisn't left referenced there indefinitely either.ApplyLocalizationsToLocalizableComponenthad no per-stringtry/catch: one disposed sub-item (locInfo.Component) owned by a still-liveILocalizableComponentcontainer threwObjectDisposedExceptionand skipped every string after it in that container, on every subsequent switch too. Each string'sApplyLocalizationToStringcall 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.Trace.WriteLineinstead of being silently swallowed -- and rather thanDebug.Fail, which a standalone repro showed terminates the process outright on net8.0-windows (Debug.WriteLinewas tried first, then switched toTrace.WriteLinesince the former is compiled out of Release builds, including the published NuGet package).Fixes #175.
Test plan
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 fromComponentCache.ToolStripItem(parented to aToolStrip, sinceIsDisposedonly reflects reality once an item belongs to one), confirming it's pruned via its own pre-check.ObjectDisposedExceptionfor a reason unrelated to its own disposal is not evicted from the cache.RefreshToolTips()with a disposed registered control.Control/ILocalizableComponentis pruned from bothComponentCacheandLocalizableComponents, not just the former.iddoesn't block localizing a live sub-item under anotheridon the sameILocalizableComponentcontainer.dotnet testpasses for all three target frameworks (net48, net462, net8.0-windows) with no regressions.🤖 Generated with Claude Code
This change is