FIX: Memory leak occurs in a WPF application when a brush object is referenced by using a DynamicResource reference

Article translations Article translations
Article ID: 2651992 - View products that this article applies to.
Expand all | Collapse all

On This Page

SYMPTOMS

Consider the following scenario:
  • You create a Microsoft Windows Presentation Foundation (WPF) application that targets the Microsoft .NET Framework 3.0 or the Microsoft .NET Framework 3.5.
  • The application defines a brush object and a color object in the XAML file of the application.
    • The brush object uses DynamicResource to reference the color object, as in the following example:
      <Application.Resources>
      
      <Color x:Key="color1">#ff4050ff</Color>
      
      <SolidColorBrush x:Key="brush1" Color="{DynamicResource color1}"/>
      
      </Application.Resources>
    • The brush object is referenced by using DynamicResource in a WPF window, in a user control, or in another object, as in the following example,
      <Window x:Class="WpfApplication6.Window1"
      
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      
      Title="Window1" Height="300" Width="300" Background="{DynamicResource brush1}">
      
      </Window>
  • You open or close the instances of the window or of the user control.

In this scenario, instances are rooted in the garbage-collected (GC) heap and not are released. Over time, a memory leak occurs as more and more instances of the window or of the user control are created. Eventually, you receive a System.OutOfMemoryException exception.

Note This issue does not occur when the application targets the Microsoft .NET Framework 4.

CAUSE

This issue occurs because the WPF run-time wires an internal event handler to the ResourceReferenceExpression object that is associated with the resource that the window or the user control is using. This event handler reference is not removed when the window or the user control is closed and set to null.

RESOLUTION

Hotfix information

A supported hotfix is now available from Microsoft. However, it is intended to correct only the problem that this article describes. Apply it only to systems that are experiencing this specific problem.

To resolve this problem, contact Microsoft Customer Support Services to obtain the hotfix. For a complete list of Microsoft Customer Support Services telephone numbers and information about support costs, visit the following Microsoft website:
http://support.microsoft.com/contactus/?ws=support
Note In special cases, charges that are ordinarily incurred for support calls may be canceled if a Microsoft Support Professional determines that a specific update will resolve your problem. The usual support costs will apply to additional support questions and issues that do not qualify for the specific update in question.

Prerequisites

To apply this hotfix, you must have the Microsoft .NET Framework 3.5.1 installed on a computer that is running one of the following operating systems:
  • Windows 7 Service Pack 1 (SP1)
  • Windows Server 2008 R2 Service Pack 1 (SP1)


Restart requirement

You must restart the computer after you apply this hotfix if the affected files are being used.

File information

The global version of this hotfix has the file attributes (or later file attributes) that are listed in the following table. The dates and times for these files are listed in Coordinated Universal Time (UTC). When you view the file information, it is converted to local time. To find the difference between UTC and local time, use the Time Zone tab in the Date and Time item in Control Panel.

For all supported x86-based versions of Windows 7
Collapse this tableExpand this table
File nameFile versionFile sizeDateTimePlatform
Presentationframework.dll3.0.6920.57385,283,84016-Dec-201101:49x86
Windowsbase.dll3.0.6920.57381,253,37616-Dec-201101:49x86
Presentationcore.dll3.0.6920.57384,218,88016-Dec-201101:49x86
Presentationframework.dll3.0.6920.57385,283,84016-Dec-201101:49x86
Presentationhostdll.dll3.0.6920.5738131,36016-Dec-201101:49x86
Presentationhost.exe4.0.40305.0296,22416-Dec-201101:49x86
Presentationhostproxy.dll4.0.31106.0100,13616-Dec-201101:49x86
Windowsbase.dll3.0.6920.57381,253,37616-Dec-201101:49x86

For all supported x64-based versions of Windows 7 and of Windows Server 2008 R2
Collapse this tableExpand this table
File nameFile versionFile sizeDateTimePlatform
Presentationcore.dll3.0.6920.57383,998,72016-Dec-201101:42x64
Presentationframework.dll3.0.6920.57384,640,76816-Dec-201101:42x86
Presentationhostdll.dll3.0.6920.5738172,32016-Dec-201101:42x64
Presentationhost.exe4.0.31106.0321,31216-Dec-201101:42x64
Presentationhostproxy.dll4.0.31106.0110,88816-Dec-201101:42x64
Windowsbase.dll3.0.6920.57381,114,11216-Dec-201101:42x86
Presentationframework.dll3.0.6920.57385,283,84016-Dec-201101:49x86
Windowsbase.dll3.0.6920.57381,253,37616-Dec-201101:49x86
Presentationcore.dll3.0.6920.57384,218,88016-Dec-201101:49x86
Presentationframework.dll3.0.6920.57385,283,84016-Dec-201101:49x86
Presentationhostdll.dll3.0.6920.5738131,36016-Dec-201101:49x86
Presentationhost.exe4.0.40305.0296,22416-Dec-201101:49x86
Presentationhostproxy.dll4.0.31106.0100,13616-Dec-201101:49x86
Windowsbase.dll3.0.6920.57381,253,37616-Dec-201101:49x86



WORKAROUND

To work around this issue, use one of the following methods:
  • Use StaticResource to reference the color object in the brush object. For example, change the first part of the code in the XAML file that is mentioned in the "Symptoms" section to the following:
    <Color x:Key="color1">#ff4050ff</Color> 
    <SolidColorBrush x:Key="brush1" Color="{StaticResource color1}"></SolidColorBrush>
  • Set the x:Shared option to false if a DynamicResource reference is used. For example, change the first part of the code in the XAML file that is mentioned in the "Symptoms" section to the following:
    <Application.Resources>
            <Color x:Key="color1">#ff4050ff</Color>
            <SolidColorBrush x:Key="brush1" x:Shared="False" Color="{DynamicResource color1}" />
        </Application.Resources>



REFERENCES

For more information about the DynamicResource markup extension, visit the following MSDN website:
General information about the DynamicResource markup extension
For more information about WPF brushes, visit the following MSDN website:
General information about WPF brushes

STATUS

Microsoft has confirmed that this is a problem in the Microsoft products that are listed in the "Applies to" section.

Properties

Article ID: 2651992 - Last Review: January 16, 2012 - Revision: 2.0
APPLIES TO
  • Microsoft .NET Framework 3.0
  • Microsoft .NET Framework 3.0 Service Pack 1
  • Microsoft .NET Framework 3.0 Service Pack 2
  • Microsoft .NET Framework 3.5
  • Microsoft .NET Framework 3.5 Service Pack 1
Keywords: 
kbqfe kbhotfixserver kbfix kbexpertiseadvanced kbsurveynew KB2651992

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com