Hi I have code here where I don\'t understand why I hit the breakpoint (see comment).
Is this a Microsoft bug of something I don\'t know or I don\'t understand prope
There are two aspects of garbage collection that you didn't count on:
The exact time at which the WeakReference.IsAlive becomes false. Your code implicitly assumes that will happen when the finalizer runs. This is not the case, it happens when the object gets garbage collected. After which the object is placed on the finalizer queue, because it has a finalizer and GC.SuppressFinalize() wasn't called, waiting for the finalizer thread to do its job. So there's a period of time where IsAlive is false but ~B() hasn't run yet.
The order in which objects get finalized is not predictable. You implicitly assume that B is finalized before A. You cannot make this assumption.
There's also a bug in the B.Dispose() method, it won't correctly count B instances when the client code explicitly disposed the object. You haven't hit that bug yet.
There is no reasonable way to fix this code. Moreover, it tests something that is already backed by hard guarantees provided by the CLR. Just remove it.