There was a nasty bug that I was seeing about a week ago. It only affected Windows systems, but it happened almost every time. The result was an exception that could not be handled, and a straight crash out of the program with no information other than a crash dump. Luckily, Visual Studio can load and debug these crash dumps and I discovered that there was an exception at the native level. The exception code was 0xC0000374 (STATUS_HEAP_CORRUPTION). The thing about this is that the program will rarely crash in the area that caused the problem with this kind of problem, however through dumb luck I was able to determine the origin of the problem. It is in this method. Can you spot it?
Indeed, I am marshalling a native
SecureString object with
Marshal. SecureStringToGlobalAllocAnsi and freeing it with
Marshal.ZeroFreeGlobalAllocUnicode. That means that I was probably overrunning the boundaries of the string when I was freeing it. This was causing the heap to become invalid. So the moral of the story is be careful how you marshal things or you will end up with hard to find bugs!