[Mono-bugs] [Bug 77146][Nor] Changed - DynamicMethods leak memory

bugzilla-daemon at bugzilla.ximian.com bugzilla-daemon at bugzilla.ximian.com
Tue Jun 12 19:56:31 EDT 2007


Please do not reply to this email- if you want to comment on the bug, go to the
URL shown below and enter your comments there.

Changed by robertj at gmx.net.

http://bugzilla.ximian.com/show_bug.cgi?id=77146

--- shadow/77146	2007-06-12 14:45:58.000000000 -0400
+++ shadow/77146.tmp.5131	2007-06-12 19:56:31.000000000 -0400
@@ -73,6 +73,55 @@
 
 
 ------- Additional Comments From vargaz at gmail.com  2007-06-12 14:45 -------
 I was wrong. The delegate keeps a reference to the DynamicMethod, so
 your patch looks right, please check it in.
 
+
+------- Additional Comments From robertj at gmx.net  2007-06-12 19:56 -------
+The following test case is crashing because the DynamicMethods
+are prematurely collected. If you insert a printf () in
+mono_reflection_destroy_dynamic_method () you'll see that
+they are collected before iterating over the stored delegates.
+
+Any idea why MonoDelegate->method_info in not able to prevent
+the DynamicMethod from being GCed?
+
+
+using System;
+using System.Collections;
+using System.Reflection;
+using System.Reflection.Emit;
+
+class Test
+{
+	delegate int SimpleMethod ();
+
+	static void Main ()
+	{
+		ArrayList list = new ArrayList ();
+
+		for (int i = 0; i < 10; i++) {
+			list.Add (GenerateMethod (i));
+		}
+
+		GC.Collect ();
+		GC.WaitForPendingFinalizers ();
+
+		foreach (SimpleMethod m in list) {
+			// m.Method, which is supposed to keep the
+			// DynMeth alive, is invalid
+			Console.WriteLine (m.Method);
+		}
+	}
+
+	static SimpleMethod GenerateMethod (int value)
+	{
+		DynamicMethod d = new DynamicMethod (value.ToString (), typeof
+(int), null, typeof (Test));
+		ILGenerator g = d.GetILGenerator ();
+		g.Emit (OpCodes.Ldc_I4, value);
+		g.Emit (OpCodes.Ret);
+		return (SimpleMethod) d.CreateDelegate (typeof (SimpleMethod));
+	}
+}
+


More information about the mono-bugs mailing list