Home »

When do I need to use GC.KeepAlive?

Question ListCategory: .NETWhen do I need to use GC.KeepAlive?
shah_kajal184 author asked 8 years ago
1 Answers
denielshakespeare5 author answered 8 years ago

It’s very unintuitive, but the runtime can decide that an object is garbage much sooner than you expect. More specifically, an object can become garbage while a method is executing on the object, which is contrary to most developers’ expectations.
using System;
using System.Runtime.InteropServices;
class Win32
{
[DllImport(“kernel32.dll”)]
public static extern IntPtr CreateEvent( IntPtr lpEventAttributes, bool bManualReset,bool bInitialState, string lpName);
[DllImport(“kernel32.dll”, SetLastError=true)]
public static extern bool CloseHandle(IntPtr hObject);
[DllImport(“kernel32.dll”)]
public static extern bool SetEvent(IntPtr hEvent);
}
class EventUser
{
public EventUser()
{
hEvent = Win32.CreateEvent( IntPtr.Zero, false, false, null );
}
~EventUser()
{
Win32.CloseHandle( hEvent );
Console.WriteLine(“EventUser finalized”);
}
public void UseEvent()
{
UseEventInStatic( this.hEvent );
}
static void UseEventInStatic( IntPtr hEvent )
{
//GC.Collect();
bool bSuccess = Win32.SetEvent( hEvent );
Console.WriteLine( “SetEvent ” + (bSuccess ? “succeeded” : “FAILED!”) );
}
IntPtr hEvent;
}
class App
{
static void Main(string[] args)
{
EventUser eventUser = new EventUser();
eventUser.UseEvent();
}
}
If you run this code, it’ll probably work fine, and you’ll get the following output:
SetEvent succeeded
EventDemo finalized
However, if you uncomment the GC.Collect() call in the UseEventInStatic()
method, you’ll get this output:
EventDemo finalized
SetEvent FAILED!

So what’s happening here? Well, at the point where UseEvent() calls UseEventInStatic(), a copy is taken of the hEvent field, and there are no further references to the EventUser object anywhere in the code. So as far as the runtime is concerned, the EventUser object is garbage and can be collected. Normally of course the collection won’t happen immediately, so you’ll get away with it, but sooner or later a collection will occur at the wrong time, and your app will fail.

Please login or Register to Submit Answer