Junfeng Zhang's Musing

things about the world, world of things

Browse by Tags

Tagged Content List
  • Blog Post: Getting where the exception is thrown in Windows Error Report for managed application without a dump

    A while ago, an internal thread shows how to get where the exception is thrown in Windows Error Report for managed application without a dump. The thread is very insightful. Shared below: Subject: RE: How to investigate Windows Error Report from managed app? FYI I actually found that there...
  • Blog Post: 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...
  • Blog Post: 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, and call...
  • Blog Post: ThreadPool.BindHandle

    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...
  • Blog Post: ThreadPool.UnsafeQueueNativeOverlapped

    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...
  • Blog Post: 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...
  • Blog Post: 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...
  • Blog Post: 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...
  • Blog Post: 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...
  • Blog Post: 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 ...
  • Blog Post: App Config’s root element should be namespace-less

    The root element for Application config file is <configuration> <?xml version="1.0" encoding="utf-8" ?> <configuration > </configuration> The application config file is shared with CLR and Windows SxS. SxS dictates that the root element is <configuration>...
  • Blog Post: Calculating the size of each GC generation

    Sos.dll does not have a command to tell the size of each GC generation. However, you can calculate the size based on the output of !eeheap -gc. For example, 0:000> !eeheap -gc Number of GC Heaps: 1 generation 0 starts at 0x1da6c430 generation 1 starts at 0x1da5c240 generation...
  • Blog Post: Figuring out object allocation graph from a managed memory dump

    when debugging a managed memory dump, we can use Sos.dll to inspect managed objects inside the dump. for example, we can do !dumpheap to list the managed objects in GC Heap, we can also use !gcroot to figure out the reference path to a particular object. However, if we want to understand the whole...
  • Blog Post: Reverse P/Invoke and exception

    P/Invoke is managed code executing a native function pointer. And Reverse P/Invoke is native code executing a managed delegate as a function pointer, mostly as a callback. When the native code executes the managed delegate function pointer, the call really calls into a CLR thunk. The thunk will do an...
  • Blog Post: Assembly Loading and Authorization

    CLR does not do anything special regarding authorization during assembly loading. It will try to access the file under the current user. However, if an assembly is already loaded, next time when the assembly is requested, CLR will return the assembly from the cache without hitting the file. When the...
  • Blog Post: Loading CLR DAC dll from a different path

    From .Net framework 2.0, CLR creates a debugger helper dll mscordacwks.dll. The dll is used by Windows Debugger to enumerate CLR data structures. Mscordacwks.dll is part of .Net framework redist. C:\Windows\Microsoft.NET\Framework\v2.0.50727>dir mscordacwks.dll Volume in drive C is Vista...
  • Blog Post: The Unusual Ref/Def Mismatch FileLoadException

    One of my colleagues has experienced a very strange Ref/Def Mismatch FileLoadException (The located assembly's manifest definition does not match the assembly reference). After a long investigation, we finally track down the cause. To explain the problem, we have to understand the sequence of assembly...
  • Blog Post: The sequence of interactions between CLR loader and fusion during Assembly.Load

    The following describes roughly the sequence of interactions between CLR loader and fusion during Assembly.Load() in .Net framework 2.0. 1. User calls Assembly.Load. 2. Loader pass the assembly reference to fusion. 3. Fusion checks to see if the assembly has already loaded. If it is, the assembly...
  • Blog Post: Lock(object) and ThreadAbortException

    We have experience some weird deadlock using managed code. We have a class with some shared resources. To protect the shared resources, we create a synchronization object, and use Lock(object) prior to access the shared resources. There is no nested lock so in theory we should never have deadlock...
  • Blog Post: Don't catch all exceptions

    One of the best practices on exception is don't catch all exceptions. There are many reasons why this is a best practice. For example, You want to look at the application state when the exception is thrown so that you can understand exactly why the exception is thrown. The exact state is often lost when...
  • Blog Post: 64 bit gacutil.exe

    32 bit gacutil.exe can install/uninstall/enumerate 64 bit assemblies for GAC, so there is really no need for 64 bit gacutil.exe.
  • Blog Post: Unknown exception 0xe0434f4e

    If you attach windows debugger to a .Net framework application, occasionally you may see debug spew of unknown exception 0xe0434f4e. (cfc.1244): Unknown exception - code e0434f4e (first chance) 0xe0434f4e is defined as EXCEPTION_HIJACK. (Copied from Rotor 2.0 sources corexcep.h) #define EXCEPTION_HIJACK...
  • Blog Post: BadImageFormatException maybe thrown when loading a valid assembly

    We saw some cases when loading a perfect valid assembly, CLR may throw BadImageFormatException occasionally. In one particular case, we discover that it is caused by anti-virus scanners. Turn off the "Protection Against Unknown Threats" feature in the anti-virus engine and the problem disappears.
  • Blog Post: Reverse P/Invoke Marshaling Performance

    Platform Invoke allows managed code to call unmanaged functions exported by an DLL. While the reverse P/Invoke allows managed code to pass a managed delegate as a callback to native code. Both P/Invoke and Reverse P/Invoke requires parameter marshaling between managed code and native code. However...
  • Blog Post: Genman32 – A tool to generate Sxs manifest for managed assembly for Registration Free COM/.Net Interop

    Attached to this article is GenMan32, a tool to generate sxs manifest for managed assembly to use in registration free COM/.Net Interop scenario. (For more information about registration free COM/.Net interop, please read http://blogs.msdn.com/junfeng/archive/2006/05/17/595140.aspx .) GenMan32...
Page 1 of 8 (186 items) 12345»