Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8917627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:32:10+00:00 2026-06-15T05:32:10+00:00

I have a WPF app that displays a text string to the UI every

  • 0

I have a WPF app that displays a text string to the UI every .5 seconds while in a certain mode. I noticed that when this happens there is a slow but steady increase in memory usage that is never released, and if I let it run long enough it will run out of memory and crash.

The code that does the drawing is below:

public void DrawDisplay( DrawingContext dc, Size size )
{
    ...

    // Draw Text
    var fontSize = 46.0;
    FormattedText ft = new FormattedText(
        message,
        CultureInfo.GetCultureInfo( "en-us" ),
        FlowDirection.LeftToRight,
        new Typeface( "Franklin Book" ),
        fontSize,
        Brushes.Black );

    ft.MaxTextWidth = size.Width - 2;
    ft.TextAlignment = TextAlignment.Center;

    // don't draw outside of box
    ft.MaxTextHeight = size.Height;

    // Center
    var height = ( size.Height - ft.Height ) / 2;

    dc.DrawText( ft, new Point( 0, height ) );

    ...
}    

The text is drawn on a canvas with the following section of code:

DrawingVisual dv = new DrawingVisual();
var size = new Size( canvas.Width, canvas.Height );

using( DrawingContext dc = dv.RenderOpen() )
{
    // draw a transparent rect so the text doesn't stretch (fixes size)
    dc.DrawRectangle( Brushes.Transparent, null, new Rect( 0, 0, size.Width, size.Height ) );

    RadioInterface.DrawDisplay( dc, size );
}

canvas.Background = new VisualBrush( dv );

I am able to “fix” this memory leak by commenting out only the “dc.DrawText(…” line. Doing this, the memory usage of the application is completely flat. Beyond this, I am not sure what to try. I tried searching for memory leaks related to DrawText but have found no results, and the code used in my function is almost identical to the example on the MSDN site for DrawText.

Has anyone seen this or see something that I am doing wrong?

  • I tried using CLRProfiler but I am not very familiar with it and the memory leak is so slow I was not able to get any useful information out of it (possibly a lack of understanding the tool).
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T05:32:12+00:00Added an answer on June 15, 2026 at 5:32 am

    So after some more reading and re-reading, I noticed the following comment from this blog post: https://blogs.msdn.com/b/jgoldb/archive/2008/02/04/finding-memory-leaks-in-wpf-based-applications.aspx?Redirected=true

    “This is a leak in WPF present in version 3.5 SP1 ONLY. This occurs when a VisualBrush, WriteableBitmap, or some select other classes are used within a Viewport3D in software rendering mode.”

    I’m using a Viewport, not a Viewport3D, but I am using software rendering and if I change the VisualBrush to a SolidColorBrush as suggested in the post to test for the leak, it dissappears. Additionally, converting from a DrawingVisual to a DrawingGroup also fixes the leak.

    Changing the second block of code to the following fixed the leak:

    DrawingVisual dg = new DrawingGroup();
    var size = new Size( canvas.Width, canvas.Height );
    
    using( DrawingContext dc = dg.Open() )
    {
        // draw a transparent rect so the text doesn't stretch (fixes size)
        dc.DrawRectangle( Brushes.Transparent, null, new Rect( 0, 0, size.Width, size.Height ) );
    
        RadioInterface.DrawDisplay( dc, size );
    }
    
    canvas.Background = new DrawingBrush( dg );
    

    I also tried using a DrawingVisual class object and re-using it over and over instead of creating a new one each time I draw, and that also fixed the leak.

    EDIT: The original reason I used DrawingVisual was it resulted in the lowest cpu usage compared to the alternatives I tried, including DrawingGroup. The difference wasn’t large, but it was consistent.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I Have a Silverlight WPF app that shows tabs of data in this case
I have a WPF app with <Image Source={Binding myUrl}> that displays images retrieved from
I have a WPF app with a textblock that displays the current time. Said
I have a WPF App that implements a ListView. I would like to show
I have a WPF app that is using the MVVM pattern. Hooking up buttons
I have a wpf app that needs to communicate(exchange data) with a custom designed
We have a WPF app that allows our users to download encrypted content and
I have a WPF app, my purpose is that the app can be automatically
I have a folder in my WPF app Images that has several .png files
Ok, I have a ResourceDictionary definition that I used for a WPF app below:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.