I'm playing around with finalizers in c# and found this example which shows that the finalizer can be called even if the constructor didn't succeed.
- How correct is this behavior?
- Is there a description of this mechanism somewhere in the documentation?
- Why is this happening?
internal class Program
{
public static void Main(string[] args)
{
Do();
GC.Collect();
Console.ReadLine();
}
public static void Do()
{
var objects = new BigObject[1000];
try
{
for (var i = 0; i < objects.Length; i++)
{
var obj = new BigObject();
objects[i] = obj;
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
}
public class BigObject
{
public BigObject()
{
Console.WriteLine(GetHashCode());
}
private Array _array = new int[1_000_000_000];
~BigObject()
{
Console.WriteLine("~" + GetHashCode());
}
}
Output:
58225482
54267293
18643596
33574638
33736294
35191196
48285313
31914638
18796293
34948909
46104728
12289376
43495525
55915408
33476626
System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.
at ConsoleApp11.BigObject..ctor() in C:\Users\t.lemeshko\source\repos\ConsoleApp11\Program.cs:line 41
at ConsoleApp11.Program.Do() in C:\Users\t.lemeshko\source\repos\ConsoleApp11\Program.cs:line 22
~55915408
~43495525
~12289376
~7880838
~46104728
~34948909
~18796293
~31914638
~48285313
~35191196
~33736294
~33574638
~18643596
~54267293
~58225482
~33476626
So whe can see there is an object with hash 7880838 has been destroyed, but not cinstructed