Trivial debugging note – using WeakReference in finalizer

Some time ago I saw a problem from a partner team in Microsoft that an InvalidOperationException is thrown from WeakReference.IsAlive. WeakReference wraps weak GC handle implemented in CLR’s Execution Engine (GC handle is also exposed by System.Runtime.InteropServices.GCHandle which supports not only weak handles, but other types too). A weak GC handle will be allocated and assigned to the…


Thread, System.Threading.Thread, and !Threads (III)

I got email asking me to explain !Threads output in details. I think this is a good question and a good topic for another installment to the series. Here is an example I’ll use for this post: 0:055> !threadsThreadCount: 202UnstartedThread: 95BackgroundThread: 1PendingThread: 0DeadThread: 47                                  PreEmptive   GC Alloc                     Lock             ID  ThreadOBJ       State     GC       Context           Domain    …


Thread, System.Threading.Thread, and !Threads (II)

With knowledge in my previous blog, we could avoid some mistakes in .NET programming. A C++ Thread is very resource heavy. It is associated with a lot of dynamically allocated memory and some OS handles. So it had better to be cleaned up ASAP after its corresponding OS thread dies. C++ Thread class has a…


Thread, System.Threading.Thread, and !Threads (I)

If you use SOS’s !Threads command during debugging a lot,  you should be familiar with such output: 0:003> !threadsPDB symbol for mscorwks.dll not loadedLoaded Son of Strike data table version 5 from “C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\mscorwks.dll”ThreadCount: 12UnstartedThread: 5BackgroundThread: 1PendingThread: 0DeadThread: 5                                  PreEmptive   GC Alloc                     Lock             ID  ThreadOBJ       State     GC       Context           Domain     Count APT Exception  0  0xb74 0x0014f230       …


Special threads in CLR

Question: How many threads does a typical managed process have when it just starts to run?   Answer: regardless how many threads the user creates, there are at least 3 threads for a common managed process after CLR starts up: a main thread which starts CLR and run user’s Main method, CLR debugger helper thread which…


Desctructor, finalizer, and Dispose – Part2.C++/CLI in Whidbey

I changed the program in previous post to use new Whidbey syntax. using namespace System; ref class RefT{public:        RefT () {Console::WriteLine (“RefT::RefT”);}    ~RefT () {Console::WriteLine (“RefT::~RefT”);}    !RefT () {Console::WriteLine (“RefT::!RefT”);}}; value class ValueT{     //constructor is not allowed for value type    //destructor is not allowed for value type}; int main(){    { //1. finalizer will be called…


Desctructor, finalizer, and Dispose – Part 1.managed C++ extension in V1.X

As a C++ fan, I’m a long time admirer for deterministic finalization. I think introduction of garbage collection to C style language by Java and .Net is a huge improvement. However, I found lose of deterministic destructor is almost unacceptable when I first enter Java/.Net world. Of course I’m used to it now, but it’s…


Trivial debugging note – what catch(…) can’t catch

One day I was debugging a problem where a Waston dialog popped up on a process. What surprised me was that on the stack where Waston was triggered, there was a unmanaged C++ function with a try-catch(…) block. To my understanding, this block should catch any user mode exception thrown in Windows, including exception from RaiseException…


Figure out variable lifetime using SOS

There is a bug in this program below, try to see if you could catch it. Test.cs (compiled to DelegateExample.exe):  using System;using System.Threading;using System.Runtime.InteropServices; class Test{ delegate uint ThreadProc (IntPtr arg);  private uint m;  public Test (uint n) {  m = n; }  uint Reflect (IntPtr arg) {  Console.WriteLine (m);  return m; }  static void Main () {  Test t = new Test (1);  ThreadProc tp…


Thread safety of Timer callbacks

I didn’t realize I’ve stopped blogging for 1 year. What a shame! Fortunately I didn’t waste the time: we ship Whidbey Beta1 and Beta2 in the past year! Now with Beta2 out of door, I have more spare time for blogging. 🙂 Today I want to talk about some interesting facts about Timer in CLR….