[tests] Test loading references from LoadFrom and LoadFile contexts
[mono-project.git] / mono / tests / sgen-bridge-gchandle.cs
blobc9a706485356efec7261e3bb244af514038d2c74
1 using System;
2 using System.Collections;
3 using System.Threading;
4 using System.Runtime.InteropServices;
7 public class Bridge {
8 public int __test;
9 public string id;
11 ~Bridge () {
12 try {Console.WriteLine ("bridge {0} gone", id);} catch (Exception) {}
18 Test scenario:
19 Alloc a bridge and create a gc handle to it
20 Get it collected.
21 Create another one and see it steal the handle of the previous one.
25 class Driver {
26 public static GCHandle weak_track_handle;
27 public static GCHandle weak_track_handle2;
29 static void CreateFirstBridge () {
30 Bridge b = new Bridge() {
31 __test = 0,
32 id = "first",
34 weak_track_handle = GCHandle.Alloc (b, GCHandleType.WeakTrackResurrection);
37 static void CreateSecondBridge () {
38 Bridge b = new Bridge() {
39 __test = 1,
40 id = "second",
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);
49 static int Main () {
50 var t = new Thread (CreateFirstBridge);
51 t.Start ();
52 t.Join ();
54 GC.Collect ();
55 GC.WaitForPendingFinalizers ();
56 Console.WriteLine ("GC DONE");
58 DumpHandle (weak_track_handle, "weak-track1");
60 t = new Thread (CreateSecondBridge);
61 t.Start ();
62 t.Join ();
64 GC.Collect ();
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!");
73 return 1;
76 return 0;