2 using System
.Collections
;
3 using System
.Threading
;
4 using System
.Runtime
.InteropServices
;
12 try {Console.WriteLine ("bridge {0} gone
", id);} catch (Exception) {}
19 Alloc a bridge and create a gc handle to it
21 Create another one and see it steal the handle of the previous one.
26 public static GCHandle weak_track_handle;
27 public static GCHandle weak_track_handle2;
29 static void CreateFirstBridge () {
30 Bridge b = new Bridge() {
34 weak_track_handle = GCHandle.Alloc (b, GCHandleType.WeakTrackResurrection);
37 static void CreateSecondBridge () {
38 Bridge b = new Bridge() {
42 weak_track_handle2 = GCHandle.Alloc (b, GCHandleType.WeakTrackResurrection);
45 static void DumpHandle (GCHandle h, string name) {
46 Console.WriteLine ("{0}
:{1:X} alloc
:{2} hasValue
:{2}
", name, (IntPtr)h, h.IsAllocated, h.Target == null);
50 var t = new Thread (CreateFirstBridge);
55 GC.WaitForPendingFinalizers ();
56 Console.WriteLine ("GC DONE
");
58 DumpHandle (weak_track_handle, "weak
-track1
");
60 t = new Thread (CreateSecondBridge);
65 GC.WaitForPendingFinalizers ();
66 Console.WriteLine ("GC DONE
");
67 DumpHandle (weak_track_handle, "weak
-track1
");
68 DumpHandle (weak_track_handle2, "weak
-track2
");
69 Console.WriteLine ("DONE
");
71 if ((IntPtr)weak_track_handle == (IntPtr)weak_track_handle2) {
72 Console.WriteLine ("FIRST HANDLE GOT DEALLOCATED
!");