HeavenWu / slimdx

Automatically exported from code.google.com/p/slimdx
MIT License
0 stars 0 forks source link

System.OutOfMemoryException in June 2010 build #693

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Hello!

This error is only thrown having run my application for a few minutes. It 
doesn't happen straight away. Here's the stack trace:

See the end of this message for details on invoking 
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' 
was thrown.
   at System.Collections.Generic.Dictionary`2.Resize()
   at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
   at SlimDX.ObjectTable.Add(ComObject comObject, ComObject owner)
   at SlimDX.ComObject.Construct(IUnknown* pointer)
   at SlimDX.DirectWrite.TextLayout.Init(Factory factory, String text, TextFormat format, Single maxWidth, Single maxHeight)
   at SlimDX.DirectWrite.TextLayout..ctor(Factory factory, String text, TextFormat format, Single maxWidth, Single maxHeight)
   at MyClass.TextLayout(String text) in J:\root\CanvasDirect2D.cs:line 597
   at MyClass.MeasureString(ChartFont f, String text) in J:\root\CanvasDirect2D.cs:line 1039

My methods look like this:
    public override System.Drawing.SizeF MeasureString(ChartFont f, string text)
    {
      this.Font = f;
      SlimDX.DirectWrite.TextLayout layout = TextLayout(text);
      SlimDX.DirectWrite.TextMetrics metrics = layout.Metrics;
      return new SizeF(metrics.WidthIncludingTrailingWhitespace, metrics.Height);
    }

    private SlimDX.DirectWrite.TextLayout TextLayout(string text)
    {
      if (this.dwfactory == null)
      {
        this.dwfactory = new SlimDX.DirectWrite.Factory(SlimDX.DirectWrite.FactoryType.Shared);
      }
      SlimDX.DirectWrite.TextLayout result = new SlimDX.DirectWrite.TextLayout(dwfactory, text, TextFormat(), XCenter * 2, YCenter * 2);
      return result;
    }

Nothing special here. 

Thank you!

Original issue reported on code.google.com by cirel...@gmail.com on 2 Jul 2010 at 11:34

GoogleCodeExporter commented 8 years ago
I presume you call MeasureString (and thus TextLayout) in a loop somewhere?

If so, the memory leak is caused by the fact that you never Dispose() of the 
Factory or TextLayout objects you allocate. SlimDX objects do not finalize 
themselves, so you're leaking all the native resources that underpin the SlimDX 
objects. This behavior is documented in the Object Lifetimes section of the 
Programming Guide in the SDK Documentation.

Original comment by josh.petrie on 2 Jul 2010 at 2:56

GoogleCodeExporter commented 8 years ago
That's great, thanks for your help!

Original comment by cirel...@gmail.com on 2 Jul 2010 at 3:10