“LockSlimInFinalizer” (Problem)
What will the following code display?
1
class Foo
2
{
3
private readonly ReaderWriterLockSlim lockSlim;
4
5
public Foo(ReaderWriterLockSlim lockSlim)
6
{
7
this.lockSlim = lockSlim;
8
lockSlim.EnterReadLock();
9
}
10
11
~Foo()
12
{
13
Console.WriteLine("~Foo: start");
14
try
15
{
16
if (lockSlim != null)
17
lockSlim.ExitReadLock();
18
}
19
catch (Exception e)
20
{
21
Console.WriteLine("Exception: " + e.GetType().Name);
22
}
23
Console.WriteLine("~Foo: finish");
24
}
25
}
26
void Main()
27
{
28
var foo = new Foo(new ReaderWriterLockSlim());
29
GC.Collect();
30
GC.WaitForPendingFinalizers();
31
}
Copied!
Solution
Copy link