[Mono-bugs] [Bug 29604][Blo] New - WeakReferences still prevent Garbage Collection

bugzilla-daemon@rocky.ximian.com bugzilla-daemon@rocky.ximian.com
29 Aug 2002 03:30:02 -0000


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 manyoso@yahoo.com.

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

--- shadow/29604	Wed Aug 28 23:30:02 2002
+++ shadow/29604.tmp.29312	Wed Aug 28 23:30:02 2002
@@ -0,0 +1,80 @@
+Bug#: 29604
+Product: Mono/Runtime
+Version: unspecified
+OS: 
+OS Details: 
+Status: NEW   
+Resolution: 
+Severity: 
+Priority: Blocker
+Component: misc
+AssignedTo: mono-bugs@ximian.com                            
+ReportedBy: manyoso@yahoo.com               
+QAContact: mono-bugs@ximian.com
+TargetMilestone: ---
+URL: 
+Cc: 
+Summary: WeakReferences still prevent Garbage Collection
+
+WeakReferences are preventing Garbage Collection.  The following illustrates the 
+problem: 
+ 
+using System; 
+using System.Threading; 
+ 
+public class MyClass 
+{ 
+	public int instanceNumber; 
+	private static int classCounter = 0; 
+	 
+	public MyClass() 
+	{ 
+		instanceNumber = classCounter++; 
+	} 
+ 
+	~MyClass() 
+	{ 
+		Console.WriteLine("MyClass instance {0} destroyed", instanceNumber); 
+	} 
+} 
+ 
+public class Testing 
+{ 
+	static void Main() 
+	{ 
+		MyClass instance;	 
+		object[] list = new object[10]; 
+	 
+		for( int i = 0; i <= 9 ; i++ ) 
+		{ 
+			instance = new MyClass(); 
+			//list[i] = instance; 
+			list[i] = new WeakReference(instance); 
+			instance = null; 
+			// Toggle the above two definitions of list[i] for the test. 
+			// The WeakReference should not prevent garbage collection, 
+			// while the other _will_ prevent garbage collection. 
+		} 
+			 
+		GC.Collect(); 
+		GC.WaitForPendingFinalizers(); 
+		Thread.Sleep(2000); 
+		while (true) { 
+			if (list[0] == typeof (WeakReference)) 
+			Console.WriteLine("Still Here :-)"); 
+		} 
+	} 
+} 
+ 
+Actual Results: 
+ 
+nothing. there is just an endless loop. 
+ 
+Expected Results: 
+ 
+when using WeakReferences the finalizers should be called. i've confirmed that this works 
+with .Net 
+ 
+How often does this happen?  
+ 
+everytime.