Merge pull request #5714 from alexischr/update_bockbuild
[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                         for (int i = 0; i < 10; i++)
22                                 new FinalizerException ();
23                         return;
24                 }
25                 MakeException (depth - 1);
26         }
27
28         public static int Main () { 
29                 AppDomain.CurrentDomain.UnhandledException += (sender, args) => {
30                         Console.WriteLine ("caught");
31                         Environment.Exit (0);
32                 };
33
34                 var t = new Thread (delegate () { MakeException (1024); });
35                 t.Start ();
36                 t.Join ();
37
38                 GC.Collect ();
39                 GC.WaitForPendingFinalizers ();
40
41                 Thread.Sleep (Timeout.Infinite); // infinite wait so we don't race against the unhandled exception callback
42
43                 return 2;
44         }
45 }