Here is a great article about GC may occur at unexpected point of code execution:
Lifetime, GC.KeepAlive, handle recycling – by cbrumme http://blogs.msdn.com/b/cbrumme/archive/2003/04/19/51365.aspx?wa=wsignin1.0
My question is how can I reproduce forced GC at the point mentioned in the article? I tried to put GC.Collect() at beginning of OperateOnHandle(), and defined destructor for class C but seems not working. Destructor is invoked always at end of program.
@Jon, thanks for advising me running out of debugger. Now I am able to reproduce the issue described in the article, using optimised Release build out of debugger. It proves this GC behaviour doesn't change since .NET v1.
Code I used:
class C1
{
// Some unmanaged resource handle
IntPtr _handle = IntPtr.Zero;
static void OperateOnHandle(IntPtr h)
{
GC.Collect();
GC.WaitForPendingFinalizers();
Console.WriteLine("After GC.Collect() call");
// Use the IntPtr here. Oops, invalid operation
}
public void m()
{
OperateOnHandle(_handle);
}
~C1()
{
// Release and destroy IntPtr here
Console.WriteLine("In destructor");
}
}
class Program
{
static void Main(string[] args)
{
C1 aC = new C1();
aC.m();
}
}
Output:
In destructor After GC.Collect() call