Bug 25559 - Memory leak in Microsoft.Win32.Registry.toKey()
Summary: Memory leak in Microsoft.Win32.Registry.toKey()
Alias: None
Product: Class Libraries
Classification: Mono
Component: mscorlib ()
Version: master
Hardware: All All
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2014-12-21 12:57 UTC by vikingcoder
Modified: 2015-03-03 16:20 UTC (History)
4 users (show)

Is this bug a regression?: ---
Last known good build:

Demonstrate memory leak sample program (1.95 KB, text/plain)
2014-12-21 12:57 UTC, vikingcoder

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description vikingcoder 2014-12-21 12:57:34 UTC
Created attachment 9159 [details]
Demonstrate memory leak sample program


   key = nkey; // key should be Disposed() prior to assignment of new value.

Also on Line 99

   return null; // key should be disposed prior to return

Sample to show leak and proposed fix attached
Comment 1 Miguel de Icaza [MSFT] 2014-12-22 10:08:47 UTC
I am not a fan of hardcoding the root key.

It seems like the Dispose() could be hitting just about any key, even those that are not owned by this piece of code.

I'll review this when I get back from vacation.
Comment 2 vikingcoder 2014-12-22 13:53:10 UTC
Thanks Miguel.

The hardcoding concerns are understood. rootKey is a local variable and the intention is to avoid Disposing of LocalMachine etc.

Additionally, I think that the ToKey() references in this file should be wrapped in a using() {}


GetValue should be

public static object GetValue (string keyName, string valueName, object defaultValue)
			using (RegistryKey key = ToKey (keyName, false)) {
				if (key == null)
					return defaultValue;
				return key.GetValue (valueName, defaultValue);
Comment 3 Miguel de Icaza [MSFT] 2015-03-03 16:20:20 UTC
Fixed on master