{"record":{"id":"142e123643096f01","repo":"dotnet/wpf","slug":"sr-textlinehasbeendisposed","errorCode":null,"errorMessage":"SR.TextLineHasBeenDisposed","messagePattern":"SR\\.TextLineHasBeenDisposed","errorType":"exception","errorClass":"ObjectDisposedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/TextFormatting/FullTextLine.cs","lineNumber":539,"sourceCode":"\n\n            /// <summary>\n            /// Draw line\n            /// </summary>\n            /// <param name=\"drawingContext\">drawing context</param>\n            /// <param name=\"origin\">drawing origin</param>\n            /// <param name=\"inversion\">indicate the inversion of the drawing surface</param>\n            public override void Draw(\n                DrawingContext      drawingContext,\n                Point               origin,\n                InvertAxes          inversion\n                )\n            {\n                ArgumentNullException.ThrowIfNull(drawingContext);\n\n                if ((_statusFlags & StatusFlags.IsDisposed) != 0)\n                {\n                    throw new ObjectDisposedException(SR.TextLineHasBeenDisposed);\n                }\n\n                MatrixTransform antiInversion = TextFormatterImp.CreateAntiInversionTransform(\n                    inversion,\n                    _metrics._formatter.IdealToReal(_paragraphWidth, PixelsPerDip),\n                    _metrics._formatter.IdealToReal(_metrics._height, PixelsPerDip)\n                    );\n\n                if (antiInversion == null)\n                {\n                    DrawTextLine(drawingContext, origin, null);\n                }\n                else\n                {\n                    // Apply anti-inversion transform to correct the visual\n                    drawingContext.PushTransform(antiInversion);\n                    try\n                    {","sourceCodeStart":521,"sourceCodeEnd":557,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/TextFormatting/FullTextLine.cs#L521-L557","documentation":"FullTextLine.Draw throws ObjectDisposedException (SR.TextLineHasBeenDisposed) when drawing a TextLine whose unmanaged resources have already been released. Disposal happens via Dispose() or when the formatter invalidates the line (e.g. ClearLayoutThrow / Dispose on the TextFormatter or re-formatting).","triggerScenarios":"Calling line.Draw(drawingContext, origin, inversion) after line.Dispose(), after TextFormatterImp was disposed (e.g. using-block around the formatter closed), or after caching a line across layout passes and the formatter disposed it.","commonSituations":"Drawing cached TextLine objects in a later render pass after the formatting scope closed; storing lines in a view-model and drawing them after Dispose was called on cleanup; using 'using' around TextFormatter then drawing lines afterwards.","solutions":["Draw the line inside the same scope in which it was formatted, before Dispose.","Do not wrap the TextFormatter in a using block if lines drawn later must remain valid.","Cache the rendered output (DrawingContext/DrawingVisual) instead of the live TextLine.","Re-format the line with TextFormatter.FormatLine if it has been disposed."],"exampleFix":"// before\nTextLine line;\nusing (var tf = TextFormatter.Create())\n{\n    line = tf.FormatLine(...);\n}\nline.Draw(dc, origin, inversion); // ObjectDisposedException\n// after\nusing (var tf = TextFormatter.Create())\n{\n    var line = tf.FormatLine(...);\n    line.Draw(dc, origin, inversion);\n}","handlingStrategy":"validation","validationCode":"void SafeDraw(TextLine line, DrawingContext dc, Point o, InvertAxes a)\n{ if (line == null) throw new ArgumentNullException(nameof(line)); line.Draw(dc, o, a); } // call before any Dispose scope closes","typeGuard":null,"tryCatchPattern":"try { line.Draw(dc, origin, inversion); }\ncatch (ObjectDisposedException)\n{\n    line = formatter.FormatLine(source, ...); // re-create\n    line.Draw(dc, origin, inversion);\n}","preventionTips":["Draw lines before the TextFormatter using-block ends.","Don't store TextLine objects beyond the layout pass; cache rendered visuals instead.","Never call Dispose on lines you still intend to draw.","Keep one TextFormatter per owner and dispose only at shutdown."],"tags":["wpf","text-formatting","object-disposed","rendering"],"backgroundTag":"invalid-state-transition","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}