Unsafe and Fixed, unmanaged code in C#

Usually, code in .NET are managed and safe code whose memory usuage are managed by .NET garbage collection. But there are times which you want to manage some low level stuff by yourself like you did in C. C# allows you to do so by using keyword unsafe. The following are description from MSDN.

A particularly interesting feature of C# is its support for non-type-safe code. Normally, the common language runtime (CLR) takes on the responsibility for overseeing the behavior of Microsoft intermediate language (MSIL) code, and prevents any questionable operations. However, there are times when you wish to directly access low-level functionality such as Win32 API calls, and you are permitted to do this, as long as you take responsibility for ensuring such code operates correctly. Such code must be placed inside unsafe blocks in our source code.
The unsafe Keyword
C# code that makes low-level API calls, uses pointer arithmetic, or carries out some other unsavory operation, has to be placed inside blocks marked with the unsafe keyword. Any of the following can be marked as unsafe:
An entire method.
A code block in braces.
An individual statement.

For the fixed keyword, it is use to allocate a piece of memory whose location is fix. Interestingly, .NET move variables and object instances around during runtime. I guess .NET do this for memory defragmentation. So, the position of your variables and instances in memory are unpreditable and may change during runtime. This is why we need fixed keyword. Here is what MSDN says:

The fixed statement prevents the garbage collector from relocating a movable variable. The fixed statement is only permitted in an unsafe context. Fixed can also be used to create fixed size buffers.

The fixed statement sets a pointer to a managed variable and “pins” that variable during the execution of the statement. Without fixed, pointers to movable managed variables would be of little use since garbage collection could relocate the variables unpredictably. The C# compiler only lets you assign a pointer to a managed variable in a fixed statement.

Unsafe and Fixed usually works together to complete certain tasks.
Here is an example which I use them to load an embedded font.
About how to embedded a file as resource is in another post HERE.

PrivateFontCollection privateFontCollection = new PrivateFontCollection();
Stream fontStream = l_thisLibrary.GetManifestResourceStream(EmbeddedResourceString);
FontFamily fontFamily = LoadFontFamily(fontStream, out privateFontCollection);
Font font = new Font(fontFamily, fontSize, FontStyle.Regular, GraphicsUnit.Pixel);
/* do something with the font*/
//release the memory when you are done with the font.
public static FontFamily LoadFontFamily(Stream stream, out PrivateFontCollection fontCollection)
            byte[] buffer = new byte[stream.Length];
            stream.Read(buffer, 0, buffer.Length);
            return LoadFontFamilyUnsafe(buffer, out fontCollection);
public static unsafe FontFamily LoadFontFamilyUnsafe(byte[] buffer, out PrivateFontCollection fontCollection)
            fixed (byte* ptr = buffer)
                fontCollection = new PrivateFontCollection();
                fontCollection.AddMemoryFont(new IntPtr(ptr), buffer.Length);
                return fontCollection.Families[0];

Static method LoadFontFamily() load a font from stream, add it into PrivateFontCollection. Static method LoadFontFamilyUnsafe() is an unsafe method which create a buffer in memory at a fix location, load the stuff from stream into that buffer.
After you are done with that fix resource in your memory, you have to explicitly dispose it. And you have to compile your project which contain unsafe code with argument unsafe.
Go to project properties->build->check allow Unsafe code.
If you have more situation which requires Unsafe and fixed, please let me know. Thank you.


Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s