WPF - error when calling Measure: Not enough storage is available to process this command. (Exception from HRESULT: 0x80070008)

0

In a WPF, single-thread application, I'm adding several FixedPages to a FixedDocument. Every FixedPage contains many other objects, like Images and TextBlocks.

Everything goes OK, but after some time (having added about 300 pages), I create a new TextBlock, fill some properties to it, and before adding it to the FixedPage, I call Measure(width, height) to ask for its desired size. During that call, I get the following Exception:

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)

Other threads seems to target the same error, but their contexts look very different. Any suggestions to solve this problem?

Thanks!

c#
wpf
comexception

1 Answer

2

I found an answer in Microsoft Visual Studio Forum. It says:

TextInterface might lead to memory leak.
The WPF team has reviewed this issue and fixed in .NET 4.6.

— post by Sachin [MSFT], 14/10/2015 at 15:03

[Reference]

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

answered on Stack Overflow Dec 27, 2016 by walterlv • edited Jun 20, 2020 by Community

User contributions licensed under CC BY-SA 3.0