none
DrawText Memory Leak? RRS feed

  • Question

  • I am experiencing an issue with a memory leak.  It's been tracked down to a call to DrawText off of a Microsoft.DirectX.Direct3D.Font object. I have been looking for answers to this issue and it seems it has something to do with the Sprite argument in the call to DrawText.  The documentation suggests that there is an issue with subsequent calls to DrawText and passing null as the sprite argument.  I changed the code to use a sprite that I create once on the first call to the Render method and it still leaks each time it's called.  It's currently leaking about 1MB/sec (which is horrendous!).

    Commenting out the second call to DrawText makes the leak go away.  Perhaps I can tweak the arguments to the Sprite.Begin() call?  Any suggestion would be appreciated!

        public override void Render(DrawArgs drawArgs)
        {
          if (Information == null || !Visible)
            return;
    
          if (Border == null)
          {
            Border = new Border(drawArgs.device);
          }
    
          if (mFont == null)
          {
            mFont = new DXFont(drawArgs.device, new Font("Calibri", 11.5f, FontStyle.Regular));
            ReCompute();
          }
          
          if (mSprite == null)
          {
            mSprite = new Sprite(drawArgs.device);
          }
    
          DrawTextFormat leftFormat = DrawTextFormat.Left | DrawTextFormat.SingleLine | DrawTextFormat.VerticalCenter;
          DrawTextFormat rightFormat = DrawTextFormat.Right | DrawTextFormat.SingleLine | DrawTextFormat.VerticalCenter;
          Rectangle rect = new Rectangle(AbsoluteLocation, new Size(Size.Width, mRowHeight));
          Rectangle clipRect = new Rectangle(ClipRectangle.Location,
            new Size(ClipRectangle.Width + BorderSize, ClipRectangle.Height));
    
          mSprite.Begin(SpriteFlags.None);
          foreach (var status in Information)
          {
            if (DisplayConfig == null || !DisplayConfig.HiddenFields.Contains(status.Name))
            {
              if (rect.Top > AbsoluteLocation.Y + clipRect.Height)
                break;
    
              if (status.IsEmpty)
              {
                rect.Height = BarHeight / 2;
    
                Rectangle bounds = Rectangle.Intersect(rect, clipRect);  
    
                if (bounds.Contains(rect))
                  Border.Draw(rect, false, BarHeight / 4);
                rect.Offset(0, BarHeight);
              }
              else
              {
                rect.Height = mRowHeight;
                Rectangle bounds = Rectangle.Intersect(rect, clipRect);
    
                mFont.DrawText(mSprite, status.Name, bounds, leftFormat, Color.Gold);
                //if (status.HasValue)
                //  mFont.DrawText(mSprite, status.Value, bounds, rightFormat, Color.White);
    
                rect.Offset(0, mRowHeight);
              }
            }
          }
          mSprite.End();
        }
    
    


    • Moved by Sheldon _Xiao Friday, July 15, 2011 10:16 AM (From:Windows Presentation Foundation (WPF))
    Thursday, July 7, 2011 1:13 AM

All replies