[Mono-list] GC bug

Fredrik Nilsson jymdman@home.se
03 Nov 2002 12:35:02 +0100


Hi,

The following code doesn't get garbage collected
right with the mono runtime (current anoncvs).

Output from mono:

>New Test1: 1
>New Test1: 2
>New Test1: 3
>New Test1: 4
>DisposeNative: 4
>New Test1: 5


Output from windows:

>New Test1: 1
>New Test1: 2
>New Test1: 3
>New Test1: 4
>DisposeNative: 4
>New Test1: 5
>DisposeNative: 5
>DisposeNative: 1
>DisposeNative: 3
>DisposeNative: 2


Regards

Fredrik Nilsson





using System;
using System.Collections;

namespace SharpTest {
	public class Test1 : IDisposable {
		static ArrayList _objects = new ArrayList ();
		int number;
		bool disposed = false;
		
		public Test1 (int nr) {
			_objects.Add (this);
			Console.WriteLine ("New Test1: " + nr);
			number = nr;
		}
		
		~Test1() {
			Dispose ();
		}
		
		public void Dispose () {
			if (disposed)
				return;
			
			DisposeNative ();
			disposed = true;
		}
		
		protected virtual void DisposeNative () {
			_objects.Remove (this);
			Console.WriteLine ("DisposeNative: " + number);
			GC.SuppressFinalize (this);
		}
	}
	
	public class Test {
		public static int Main (string[] args) {
			Test1 a = new Test1 (1);
			Test1 b = new Test1 (2);
			Test1 c = new Test1 (3);
			Test1 d = new Test1 (4);
			d.Dispose ();
			Test1 e = new Test1 (5);

			return 0;
		}
	}
}