[runtime] Fixed get_process_module module name.
[mono.git] / mono / tests / finalizer-exception.cs
1 using System;
2 using System.Threading;
3
4 public class FinalizerException {
5
6         ~FinalizerException () {
7                 throw new Exception ();
8         }
9
10         static IntPtr aptr;
11
12         /*
13          * We allocate the exception object deep down the stack so
14          * that it doesn't get pinned.
15          */
16         public static unsafe void MakeException (int depth) {
17                 // Avoid tail calls
18                 int* values = stackalloc int [20];
19                 aptr = new IntPtr (values);
20                 if (depth <= 0) {
21                         new FinalizerException ();
22                         return;
23                 }
24                 MakeException (depth - 1);
25         }
26
27         public static int Main () { 
28                 AppDomain.CurrentDomain.UnhandledException += (sender, args) => {
29                         Console.WriteLine ("caught");
30                         Environment.Exit (0);
31                 };
32
33                 var t = new Thread (delegate () { MakeException (1024); });
34                 t.Start ();
35                 t.Join ();
36
37                 GC.Collect ();
38                 GC.WaitForPendingFinalizers ();
39
40                 Thread.Sleep (Timeout.Infinite); // infinite wait so we don't race against the unhandled exception callback
41
42                 return 2;
43         }
44 }