Compiler aided overloading

I was playing with xUnit.net for a pet project of mine. I wasn’t writing test cases with xUnit rather I was using the underlying xUnit engine to discover test cases and invoke calls that execute desired test cases. Forget the details of what I was doing, let us talk about it in a different post. But for now, I was consuming xUnit’s backend library.

XunitFrontController is the gateway to xUnit’s world; AFAIK. You create an instance of the controller specifying the target assembly.

var xfc = new XunitFrontController(
  AppDomainSupport.IfAvailable,
  "{full path of the assembly where the test cases reside}"
);

Continue reading Compiler aided overloading

.NET for the next generation

It was about a decade ago when Visual Studio .NET 2002 was launched. Having worked with Visual Studio 6 until then, the new interface was refreshing and powerful along with .NET and the suite of languages, tools and technologies. If you were there, you would have felt times were changing. Beyond the cool and modern interface, Visual Studio .NET 2002 had a lot more to offer compared to Visual Studio 6 — .NET. It was an exciting time for me back then.

Continue reading .NET for the next generation

Mutating Strings

Today, we question our beliefs! Is string really immutable?

string message = "Hello World!";

Console.WriteLine(message);        // Prints "Hello World!"

unsafe {  
    int length = message.Length;

    fixed (char *p = message) {
        for (int index = 0; index < length; ++index) {
            *(p + index) = '?';
        }
    }
}

Console.WriteLine(message);     // Prints what? See for yourself!

Continue reading Mutating Strings

To Hold or Not to Hold – A story on Thread references !!!

void SomeMethod(int x, double y) {
    // some code
    ....
    new Thread(ThreadFunc).Start();
}

What do you think about the code above?

Some may say nothing seems to be wrong. Some may say there is not enough information to comment. A few may say that it is awful to spin off a thread like that (the last line of the method), and that there is a probability for the thread to be garbage collected at an unexpected point of execution. That is something interesting to discuss about.

Before presenting my thoughts and supporting facts, the short answer is NO. A thread spun off like that will not be garbage collected as we expect, although one should be morally insane to write such code. Alright, let us discuss.

The Thread class is like any other reference type in the BCL. When an instance of a reference type holds no more outstanding references, it is a candidate to be garbage collected. Even worse, it could become a candidate even while it is executing an instance method, while there are no more outstanding references to that instance. If such facts are considered, then the thread created at free will in the above code is bound to be collected anytime while executing the associated ThreadFunc. Let us try that with simple sample application.

static void Main(string[] args) 
{
   Console.WriteLine("The Beginning...."); 

   // ThreadFunc inlined as anonymous delegate
   new Thread(delegate() 
   {
      for (int i = 0; i < 1000; i++) 
      {       
         var obj = new Junk(i, i, i.ToString()); 
         Console.WriteLine("{0}, ", i); 
         Thread.Sleep(100); 

         if (i % 10 == 0) 
         {
            GC.Collect(); 
            GC.WaitForPendingFinalizers();
         }

      }    
   }).Start(); 

   GC.Collect(); 
   GC.WaitForPendingFinalizers(); 

   Console.WriteLine("At the end!");
}

In the sample application above, I have forced garbage collections and also waited for pending finalizers at two important points of execution – 1) at the end of main thread 2) in the course of execution of the thread delegate. I have made sure that there is enough chance of GC kicking in by creating some Junk objects too. Those are some of the important triggers for GC to kick in.

If you run the application, you will see that the application does not quit until the loop runs to completion; although the main thread runs to completion – prints At the end!. Now, let us not enter an argument that the thread is a foreground thread and application shall not quit until all foreground threads have finished executing. Yes, if the thread in the above code was a background thread, the application would have quit before the loop completed. But then we would have created a reference to set it as a background thread, and we would have to stop the discussion there since we are talking about threads created\started without holding a reference. Besides, the context of the problem is not application exit but application running.

Alright, let me rephrase it in a way that is relevant to our context – If you run the application, you will see that the thread function runs to completion successfully (loops 1000 times, creates 1000 objects, waits for 100ms each iteration of the loop, triggers garbage collection/waits for pending finalizers during execution). If thread had been garbage collected, it would not have run a 1000 iterations successfully. So does that mean the CLR has a soft corner for Thread types? Seems so.

If you drill down the Thread type using Reflector, you will see that it neither implements IDisposable nor afinalizer. How could an object not implement cleanup mechanism, escape the almighty garbage collector, and still not cause any havoc? That is weird! It gives the impression that we might be leaking the underlying native thread resource. Obviously, the CLR folks are not that careless or we would not be running applications today written in managed code. My common sense told me that there is some trick played behind the scenes such that a reference to every thread is somehow maintained and the clean up is thus taken care of.

So Ananth and I rolled up our sleeves to ponder for the evidence inside the runtime using SSCLI. It is tough to project a few snippets of code or so from SSCLI to show you Here, this is the evidence. However, I can share what we saw. When a thread is created, a reference to the thread object is added to a static list maintained by the framework. Thus a reference is established whether the user code holds it or not. When a thread is started, there is a bit of framework code executed, which then turns over control to our thread delegate. When our thread delegate finishes execution (normally or abnormally), it returns to the caller inside the framework code, which takes care of removing the reference from the static list. The framework code then does some cleanup which involves closing the thread handle and such. Only then, does a thread object become a candidate for garbage collection, which finally has nothing specific to cleanup. I think the CLR folks are smart enough (obviously) to do it this way. Because 1) threads are really special resources whose behavior is a bit different than other native resources 2) Thread type is a just wrapper for over the original native\OS thread.

We have seen the evidence. Now, let us discuss something about morale. When you take a look at the code with thread spun off at free will, don’t you wink twice? Doesn’t it raise a lot of questions about correctness, safety etc. Obviously, it is not a good practice. Just because there is something in the framework to take care of, it does not unleash us to do such things. Agreed that SSCLI does not lie but is it completely safe for our application code to rely on some very intrinsic detail of the framework code? I don’t think so.

The intent of the code like SomeMethod seems to fire (off a thread to do some processing) and forget, since it is not interested in holding a reference to the thread object. And it is very well possible that SomeMethod could be called few or many times, and we would be creating new threads just to fire and forget. Haven’t we heard that threads are expensive resources? Why did people come up with idea of thread pool? Thread pool is the apt choice for such fire and forget or processing that do not require thread affinity.

Next time, you see such code, if you have the authority to change it, correct it. If not, speak to the developer who wrote the code. Start with a soft and warm conversation and explain him the morale or show him this post (a little marketing!). Make sure that the conversation does not become aggressive (getting the developer defensive). Even after such warm conversations, the developer does not have the brains to take your side, shoot him! Just kidding.

Well, that is something I wanted to share. It is now your turn to comment and/or correct. Please share your thoughts if you have found or know of any other evidences about thread references and related. I am sure it would be an interesting and worthy discussion.

sizeof vs Marshal.SizeOf !!!

There are two facilities in C# to determine the size of a type – sizeof operator andMarshal.SizeOf method. Let me discuss what they offer and how they differ. Pardon me if I happen to ramble a bit.

Before we settle the difference between sizeof and Marshal.SizeOf, let us discuss why would we want to compute the size of a variable or type. Other than academic, one typical reason to know the size of a type (in a production code) would be allocate memory for an array of items; typically done while using malloc. Unlike in C++ (or unmanaged world), computing the size of a type definitely has no such use in C# (managed world). Within the managed application, size does not matter; since there are types provided by the CLR for creating\managing fixed size and variable size (typed) arrays. And as per MSDN, the size cannot be computed accurately. Does that mean we don’t need to compute the size of a type at all when working in the CLR world? Obviously no, else I would not be writing this post.

Value Types: User-defined value (and reference types) are composed of the primitive value types exposed by the compiler, most of which exist as keywords – int, bool, char, long, double etc. Since the primitive value types are exposed by the compiler, their sizes are (pre-)defined by the compiler (based on the platform on which the CLR runs). The compiler allows querying the sizes of the primitive value types the sizeof operator. The sizeof operator returns the size of the type in bytes as allocated by the CLR (on the current platform). ReferMSDN for the sizes of primitive types.

However, the sizeof cannot be freely used with user-defined value types (struct) but only if the following conditions are true:-

  • The size of the struct is requested from within an unsafe block.

  • The struct does not contain a reference type as its member.
    Since the size of a reference type cannot be computed (see Reference Types below), the size of the struct cannot be computed too.

  • The struct is not a generic value type.
    sizeof should be imagined as a compile-time construct. That implies the type for which the size is queried should be known at compile time. When we say sizeof(GenValueType), the (closed) type definition GenValueType is not available at compile time but only at runtime. Hence the compiler does not allow computing the size of a generic value type. But the size of an instance of the same closed type may be determined. But ideally it does not sound convincing to me because the compiler uses the MSIL sizeof instruction to computer the size, instead of hard coding the size (as is done for primitive types).

Besides, the subtle and bitter thing is that the size depends on other factors such as the pack size used (StructLayout.Pack) or character set (StructLayout.CharSet) applied on the type definition or the fixed size specified (StructLayout.Size). Unlike in C++, sizeof accepts only a (closed) type known at compile time and not variables.

Reference Types: The sizeof operator cannot be used on reference types. As per MSDN, the size can be either misleading or meaningless for reference types. Consider a class (reference type) SomeClass containing a char and a string. Reference types are basically (C++) pointer like. When computing the size of SomeClass, which aspect of the string member should be consider – the reference (4 bytes) or the value (n bytes)? Also, every .NET object incurs a 16 (I guess) byte header overhead. Should we consider the header size too and the same question applies here too – 16 bytes or the mazy data structure that the header actually refers to. So for such reasons, it does not make sense to determine the size of a reference type using sizeof (at least at compile time). The other way of putting this is sizeof works only for POD types.

Given all this uncertainty in computing the size of a type (using sizeof), will there ever be a need then? In a broader sense, there is one situation. That is when the data is passed out of the managed application – Interop or custom serialization and such. For example, the managed application might want to allocate unmanaged memory for creating\filling a data structure for calling a native API, which takes the data structure as its input or would be populating it with output.

Let us enter the second half (or the better half) – Marshal.SizeOf. Unlike sizeof (C# keyword), this one is offered from the BCL. This method returns the size (in bytes) of the type or its instance if it had to exist in the unmanaged world. This method has two overloads – one taking the type as input and the other an instance. Let us say we want to allocate some memory in the unmanaged heap to call a native API (SendMessage orVirtualAlloc or ReadProcessMemory). In many cases, the amount of memory to be allocated is the equivalent of a Win32 structure –LVITEM, STARTUPINFO or one such. In such situations, Marshal.SizeOf method has to be used { int x = Marshal.SizeOf(typeof(LVITEM)); }, which returns the size of the structure depending on the StructLayoutAttribute applied.

Can Marshal.SizeOf method be used on reference and value types? It can be used on any value type but will throw an exception at runtime if the value type contains a reference type. And the error makes sense – Type cannot be marshaled as an unmanaged structure; no meaningful size or offset can be computed. Otherwise, it can be used with primitive or user-defined value types. It is allowed to be used with reference types only if the type layout is specified to be LayoutKind.Sequential or LayoutKind.Explicit; else the same exception above will be thrown at runtime.

It is possible that the size returned by sizeof and Marshal.SizeOf are different, as with the case of char. sizeof(char) is 2 since CLR is an Unicode beast. Marshal.SizeOf(char) will return 1 since a char in the unmanaged world takes up one byte. However,Marshal.SizeOf(SomeStruct) may report to that its char member consumes two bytes (by default) or made to take up one byte (if the StructLayout.CharSet=CharSet.Ansi).

Do I need to conclude something?

OrderedThreadPool – Task Execution In Queued Order !!!

I would not want to write chunks of code to spawns threads and perform many of my background tasks such as firing events, UI update etc. Instead I would use the System.Threading.ThreadPool class which serves this purpose. And a programmer who knows to use this class for such cases would also be aware that the tasks queued to the thread pool are NOT dispatched in the order they are queued. They get dispatched for execution in a haphazard fashion.

In some situations, it is required that the tasks queued to the thread pool are dispatched (and executed) in the order they were queued. For instance, in my (and most?) applications, a series of events are fired to notify the clients with what is happening inside the (server) application. Although the events may be fired from any thread (asynchronous), I would want them or rather the client would be expecting that the events are received in a certain order, which aligns with the sequence of steps carried out inside the server application for the requested service. So sequential execution of the queued tasks is not something one must not wish for.

Enough talking…….eat code.

using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace System.Threading
{
   struct ThreadPoolTaskInfo
   {
      public readonly WaitCallback CallbackDelegate;
      public readonly object State;

      public ThreadPoolTaskInfo(WaitCallback wc, object state)
      {
         Debug.Assert(wc != null);
         CallbackDelegate = wc;
         State = state;
      }
   }

   class OrderedThreadPool
   {
      private Queue workItemQ = new Queue();

      public void QueueUserWorkItem(WaitCallback wcbDelegate, object state)
      {
         lock (workItemQ)
         {
            workItemQ.Enqueue(new ThreadPoolTaskInfo(wcbDelegate, state));

            if (workItemQ.Count == 1)
            {
               ThreadPool.QueueUserWorkItem(LoopWork);
            }
         }
      }

      private void LoopWork(object notUsed)
      {
         WaitCallback wcb = null;
         object state = null;

         lock (workItemQ)
         {
            if (workItemQ.Count == 0)
            {
               return;
            }

            ThreadPoolTaskInfo tptInfo = workItemQ.Dequeue();
            state = tptInfo.State;
            wcb = tptInfo.CallbackDelegate;
            Debug.Assert(wcb != null);
         }

         try
         {
            wcb(state);
         }
         finally
         {
            ThreadPool.QueueUserWorkItem(LoopWork, notUsed);
         }
      }
   }
}

The above class wraps the System.Threading.ThreadPool and offers the facility of execution of tasks in the order they are queued. Hope that is useful!

The Surprising Finalize Call !!!

Guess the output of the following program:-

class SomeClass : IDisposable
{
public SomeClass()
{
Trace.WriteLine("SomeClass - Attempting instance creation");
throw new Exception("Ohh !!! Not Now");
}

public void Dispose()
{
Trace.WriteLine("SomeClass::Dispose");
}

~SomeClass()
{
Trace.WriteLine("SomeClass::Finalizer");
}
}

int Main(string args[]){
try{
SomeClass sc = new SomeClass();
}catch(Exception ex){
Trace.WriteLine("Main - {0}", ex.Message);
}
}

This will be the output of the program:-

SomeClass - Attempting instance creation
Ohh !!! Not Now SomeClass::Finalizer

If you are surprised with the last line of the output, that will be the intent of our discussion. In the .NET [managed] world, the garbage collector is entirely responsible for memory management – allocation and deallocation. In C#, an instance of a class is created using the new keyword. When an instance creation is requested, first memory for the instance is allocated followed by a call to the [appropriate] constructor of the class.

To explain the surprising output, the constructor is called after the memory is allocated by the GC. When the constructor throws exception, the object or resource creation is interrupted but the memory cannot deallocated instantly since the GC is entirely responsible for memory deallocation. The GC follows a complex and non-deterministic style for deallocating or reclaiming an allocated chunk of memory. The finalizer method is the last call made on a managed object just before reclaiming memory. Hence in the above case, the finalizer is being called before reclaiming the memory allocated for an instance of SomeClass.

The above behaviour is very much different from the unmanaged C++ where when the instance creation is interrupted [by throwing an exception], the allocated memory is deallocated and reclaimed instantaneously. Also the destructor is not called in this case.

P.S: Thinking of a more detailed post on non-deterministic destruction.