dotnet/wpf · error · ObjectDisposedException
SR.HyphenatorDisposed
Error message
SR.HyphenatorDisposed
What it means
NaturalLanguageHyphenator.AnalyzeText throws ObjectDisposedException with SR.HyphenatorDisposed when the hyphenator's native resource has already been released via Dispose. Once disposed, the native NlHyphenate handle (_hyphenatorResource) is invalid, so the API refuses to run rather than corrupt memory or crash native code.
Solutions
- Stop using the hyphenator instance after calling Dispose; create a new NaturalLanguageHyphenator for subsequent analysis
- Check the _disposed/IsDisposed state before calling AnalyzeText, or guard with try-catch for ObjectDisposedException
- Fix disposal ordering so text layout/hyphenation finishes before the hyphenator is disposed
Example fix
// before hyphenator.Dispose(); var positions = hyphenator.AnalyzeText(text, length); // after var positions = hyphenator.AnalyzeText(text, length); hyphenator.Dispose();
Defensive patterns
Strategy: try-catch
Validate before calling
if (hyphenator == null) return null; // also avoid use after Dispose by tracking lifetime
Type guard
bool IsUsable(NaturalLanguageHyphenator h) => h != null && !h.IsDisposed;
Try / catch
try { return hyphenator.AnalyzeText(text, length); } catch (ObjectDisposedException) { return null; } Prevention
- Call Dispose only after all text layout work is complete
- Do not share one hyphenator across disposing components
- Re-create the hyphenator instead of reusing a disposed instance
When it happens
Trigger: Calling AnalyzeText on a hyphenator instance after Dispose() has been called on it; typically reusing a cached/static hyphenator whose Dispose ran during teardown or page unload while text layout still runs.
Common situations: Hyphenation enabled on a TextBox/FlowDocument while the owning document or control is being disposed on a background or layout thread; races where one component disposes the shared hyphenator while another still formats text.
Understand the failure class
Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.
Related errors
- EncryptedPackageEnvelope object was disposed.
- FixedPageReader
- NLGSpellerInterop.SpellerSegment
- NLGSpellerInterop.SpellerSentence
- SR.ByteRangeDownloaderDisposed
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/909b1a03b3b86f73.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/NaturalLanguageHyphenator.cs:121
CultureInfo textCulture
)
{
Invariant.Assert(
characterSource != null
&& characterSource.Length > 0
&& length > 0
&& length <= characterSource.Length
);
if (_hyphenatorResource == IntPtr.Zero)
{
// No hyphenator available, no service delivered
return null;
}
if (_disposed)
{
throw new ObjectDisposedException(SR.HyphenatorDisposed);
}
byte[] isHyphenPositions = new byte[(length + 7) / 8];
UnsafeNativeMethods.NlHyphenate(
_hyphenatorResource,
characterSource,
length,
((textCulture != null && textCulture != CultureInfo.InvariantCulture) ? textCulture.LCID : 0),
isHyphenPositions,
isHyphenPositions.Length
);
return new HyphenBreaks(isHyphenPositions, length);
}
/// <summary>
/// Private implementation of TextLexicalBreaks that encapsulates hyphen opportunities withinView on GitHub (pinned to 81131a70a4)