WPF - 调用度量值时出错:没有足够的存储空间可用于处理此命令.(HRESULT的例外:0x80070008)

本文关键字:命令 处理 用于 HRESULT 0x80070008 存储空间 出错 度量 WPF 调用 | 更新日期: 2023-09-27 18:33:14

在WPF单线程应用程序中,我正在向FixedDocument添加多个FixedPages。每个 FixedPage 都包含许多其他对象,如图像和文本块。

一切正常,但一段时间后(添加了大约 300 页),我创建了一个新的 TextBlock,为其填充一些属性,在将其添加到 FixedPage 之前,我调用 Measure(宽度、高度)来询问其所需的大小。在那次通话中,我收到以下异常:

System.Runtime.InteropServices.COMException was unhandled by user code
  ErrorCode=-2147024888
  HResult=-2147024888
  Message=Not enough storage is available to process this command. (Exception from HRESULT: 0x80070008)
  Source=mscorlib
  StackTrace:
       at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
       at System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(Int32 errorCode, IntPtr errorInfo)
       at MS.Internal.Text.TextInterface.Native.Util.ConvertHresultToException(Int32 hr)
       at MS.Internal.Text.TextInterface.FontFace.GetDesignGlyphMetrics(UInt16* pGlyphIndices, UInt32 glyphCount, GlyphMetrics* pGlyphMetrics)
       at System.Windows.Media.GlyphTypeface.GlyphMetrics(UInt16* pGlyphIndices, Int32 characterCount, GlyphMetrics* pGlyphMetrics, Double emSize, TextFormattingMode textFormattingMode, Boolean isSideways)
       at System.Windows.Media.GlyphTypeface.GetGlyphMetricsAndIndicesOptimized(UInt32* pCodepoints, Int32 characterCount, Double emSize, UInt16[] glyphIndices, GlyphMetrics[] glyphMetrics, TextFormattingMode textFormattingMode, Boolean isSideways)
       at System.Windows.Media.GlyphTypeface.GetGlyphMetricsOptimized(CharacterBufferRange characters, Double emSize, UInt16[] glyphIndices, GlyphMetrics[] glyphMetrics, TextFormattingMode textFormattingMode, Boolean isSideways)
       at System.Windows.Media.Typeface.CheckFastPathNominalGlyphs(CharacterBufferRange charBufferRange, Double emSize, Double scalingFactor, Double widthMax, Boolean keepAWord, Boolean numberSubstitution, CultureInfo cultureInfo, TextFormattingMode textFormattingMode, Boolean isSideways, Boolean breakOnTabs, Int32& stringLengthFit)
       at MS.Internal.TextFormatting.SimpleRun.CreateSimpleTextRun(CharacterBufferRange charBufferRange, TextRun textRun, TextFormatterImp formatter, Int32 widthLeft, Boolean emergencyWrap, Boolean breakOnTabs)
       at MS.Internal.TextFormatting.SimpleRun.Create(FormatSettings settings, CharacterBufferRange charString, TextRun textRun, Int32 cp, Int32 cpFirst, Int32 runLength, Int32 widthLeft, Int32 idealRunOffsetUnRounded)
       at MS.Internal.TextFormatting.SimpleTextLine.Create(FormatSettings settings, Int32 cpFirst, Int32 paragraphWidth)
       at MS.Internal.TextFormatting.TextFormatterImp.FormatLineInternal(TextSource textSource, Int32 firstCharIndex, Int32 lineLength, Double paragraphWidth, TextParagraphProperties paragraphProperties, TextLineBreak previousLineBreak, TextRunCache textRunCache)
       at MS.Internal.TextFormatting.TextFormatterImp.FormatLine(TextSource textSource, Int32 firstCharIndex, Double paragraphWidth, TextParagraphProperties paragraphProperties, TextLineBreak previousLineBreak, TextRunCache textRunCache)
       at MS.Internal.Text.Line.Format(Int32 dcp, Double width, TextParagraphProperties lineProperties, TextLineBreak textLineBreak, TextRunCache textRunCache, Boolean showParagraphEllipsis)
       at System.Windows.Controls.TextBlock.MeasureOverride(Size constraint)
       at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
       at System.Windows.UIElement.Measure(Size availableSize)

其他线程似乎针对相同的错误,但它们的上下文看起来非常不同。有什么建议可以解决这个问题吗?

谢谢!

WPF - 调用度量值时出错:没有足够的存储空间可用于处理此命令.(HRESULT的例外:0x80070008)

我在Visual Studio论坛中找到了答案Microsoft。它说:

文本接口可能会导致内存泄漏.
WPF 团队已审查此问题并在 .NET 4.6 中修复。

— 发布者 Sachin [MSFT], 14/10/2015 at 15:03

[参考资料]

https://social.msdn.microsoft.com/Forums/vstudio/en-US/350a8d21-f361-4983-9bc3-65c71a78cb52/comexception-this-command-is-not-enough-memory-available?forum=wpf

https://connect.microsoft.com/VisualStudio/feedback/details/1468770/exception-with-textinterface