Be careful about exception after resource allocation

The following is a common code pattern         Resource resource = GetResource();         DoWork();                    return resource; If DoWork() throws exception, the resource will be leaked. We need to guard against this. For example         bool success = false;         Resource resource = GetResource();         try         {             DoWork();             success = true;            …


Exception Filter

C# does not support exception filter. However, VB and IL support it. To add exception filter to C#, we can build a function in VB or IL, then call it in C#. The example below is one way we may implement the function to support exception filter. It asks the caller to provide four delegates,…



I mentioned that we can use ThreadPool.BindHandle to implement asynchronous IO. Here are roughly the steps necessary to make it happen: 1.       Create an overlapped file handle             SafeFileHandle handle = CreateFile(                                 filename,                                 Win32.GENERIC_READ_ACCESS,                                 Win32.FILE_SHARE_READ | Win32.FILE_SHARE_WRITE | Win32.FILE_SHARE_DELETE,                                 (IntPtr)null,                                 Win32.OPEN_EXISTING,                                 Win32.FILE_FLAG_OVERLAPPED,                                 new SafeFileHandle(IntPtr.Zero, false));         [DllImport(“kernel32.dll”, CharSet…



CLR’s thread pool has two pools of threads. The first pool is used by ThreadPool.QueueUserWorkItem. The second pool is an IoCompletionPort thread pool used by ThreadPool.BindHandle and ThreadPool.UnsafeQueueNativeOverlapped. ThreadPool.BindHandle is used by CLR to implement asynchronous IO. For example, FileStream uses it to implement BeginRead/BeginWrite. Developers can take advantage of it too. We will talk…


Conversion between System.String and char *

We can convert a char * to System.String with System.String’s constructor string str = new string((char*)p); And for the reverse: fixed(char *p = str){} Why do we care about conversion between System.String and char *? From this article, this is the fastest way to marshal strings between managed and native boundary.


Managed ThreadPool vs Win32 ThreadPool (pre-Vista)

The following is a conversation between me and a CLR dev. The conversation is very informative so I quote it here. From:  Sent: To: Subject: RE: ThreadPool.QueueUserWorkItem   There might be some confusion here around the meaning of the term “I/O Thread.”  In the Windows thread pool (the old one, not the new Vista thread…


Event Handles “leak”

On our stress run, we saw our process’ handle count steadily increases until certain point, then it stabilizes. However the number of handles is high. Most of those handles are Event handles. We are concerned about it. So we went off and did some investigation. Turns out the Event handles are coming from the use…


Consider Creating a new class for locking

C# provides keyword lock for synchronized access.   A good practice is to create a private object for locking purpose. For example, public class LockExample {     private object syncObject = new object();       public void SynchronizedMethod()     {         lock (syncObject)         {         } }} It may make sense to create a…


A case study of a NullReferenceException

We are seeing a NullReferenceException in our stress program. The investigation process may be helpful to some folks.   Thread 22 is showing a Watson dialog.   This is a register corruption.   0:022> kp ChildEBP RetAddr  091bc9a0 76961220 ntdll!ZwWaitForSingleObject(void)+0x15 091bca10 76961188 kernel32!WaitForSingleObjectEx(void * hHandle = 0x00001034, unsigned long dwMilliseconds = 0x4e20, int bAlertable =…