FIX: Memory Leak When Throwing Exceptions from Nested Exception Blocks

This article has been archived. It is offered "as is" and will no longer be updated.
When an exception error is thrown from a catch clause, the first exception object will leak.

For example, when an error occurs while the program is running, you might want to use a try-catch statement to handle the exception. However, if the code that handles the exception in the catch clause throws another exception, the first exception object will never be deleted by the .NET Framework garbage collector. This causes a memory leak in the calling process.
A supported fix is now available from Microsoft, but it is only intended to correct the problem that is described in this article. Apply it only to computers that are experiencing this specific problem. This fix may receive additional testing. Therefore, if you are not severely affected by this problem, Microsoft recommends that you wait for the next .NET Framework service pack that contains this fix.

To resolve this problem immediately, contact Microsoft Product Support Services to obtain the fix. For a complete list of Microsoft Product Support Services phone numbers and information about support costs, visit the following Microsoft Web site: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 typical support costs will apply to additional support questions and issues that do not qualify for the specific update in question.

The English version of this fix has the file attributes (or later) 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 tool in Control Panel.
   Date         Time   Version       Size       File name   --------------------------------------------------------------   16-Nov-2002  04:05  1.0.3705.378     69,632  Corperfmonext.dll   16-Nov-2002  14:28  1.0.3705.378  1,953,792  Mscorlib.dll   16-Nov-2002  03:56                   10,272  Mscorlib.ldo   16-Nov-2002  04:04  1.0.3705.378  2,269,184  Mscorsvr.dll   16-Nov-2002  04:04  1.0.3705.378  2,269,184  Mscorwks.dll


Steps to Reproduce the Behavior

Note The following is minimal information that is relevant to demonstrate the problem that is discussed in this article.

To reproduce the problem, include the following code in a Visual C# .NET project, and then run it directly (not in the IDE or in any other debugger):
for (;;){	try	{		throw new System.Exception("I leak.");	}	catch	{		try		{			throw new System.Exception("I do not leak.");		}		catch		{		}	}	Console.Write("Press ENTER to iterate.");	Console.ReadLine();}
You will notice that this code leaks 2 garbage collection handles for each iteration.

You can monitor this behavior by looking at the following PerfMon counters:
  • .NET CLR Memory - #Bytes in all Heaps will increase per iteration
  • .NET CLR Memory - #GC Handles will increment by 2 per iteration
For additional information about how to use PerfMon, click the following article number to view the article in the Microsoft Knowledge Base:
248345 How to Create a Log Using System Monitor in Windows 2000
Microsoft has confirmed that this is a problem in the Microsoft products that are listed at the beginning of this article.

Article ID: 810178 - Last Review: 10/26/2013 17:48:28 - Revision: 2.4

  • Microsoft .NET Framework 1.0 Service Pack 2
  • kbnosurvey kbarchive kbhotfixserver kbqfe kbnetframe100presp3fix kbfix kbbug KB810178