IanG on Tap

Ian Griffiths in Weblog Form (RSS 2.0)

Blog Navigation

April (2018)

(1 item)

August (2014)

(1 item)

July (2014)

(5 items)

April (2014)

(1 item)

March (2014)

(1 item)

January (2014)

(2 items)

November (2013)

(2 items)

July (2013)

(4 items)

April (2013)

(1 item)

February (2013)

(6 items)

September (2011)

(2 items)

November (2010)

(4 items)

September (2010)

(1 item)

August (2010)

(4 items)

July (2010)

(2 items)

September (2009)

(1 item)

June (2009)

(1 item)

April (2009)

(1 item)

November (2008)

(1 item)

October (2008)

(1 item)

September (2008)

(1 item)

July (2008)

(1 item)

June (2008)

(1 item)

May (2008)

(2 items)

April (2008)

(2 items)

March (2008)

(5 items)

January (2008)

(3 items)

December (2007)

(1 item)

November (2007)

(1 item)

October (2007)

(1 item)

September (2007)

(3 items)

August (2007)

(1 item)

July (2007)

(1 item)

June (2007)

(2 items)

May (2007)

(8 items)

April (2007)

(2 items)

March (2007)

(7 items)

February (2007)

(2 items)

January (2007)

(2 items)

November (2006)

(1 item)

October (2006)

(2 items)

September (2006)

(1 item)

June (2006)

(2 items)

May (2006)

(4 items)

April (2006)

(1 item)

March (2006)

(5 items)

January (2006)

(1 item)

December (2005)

(3 items)

November (2005)

(2 items)

October (2005)

(2 items)

September (2005)

(8 items)

August (2005)

(7 items)

June (2005)

(3 items)

May (2005)

(7 items)

April (2005)

(6 items)

March (2005)

(1 item)

February (2005)

(2 items)

January (2005)

(5 items)

December (2004)

(5 items)

November (2004)

(7 items)

October (2004)

(3 items)

September (2004)

(7 items)

August (2004)

(16 items)

July (2004)

(10 items)

June (2004)

(27 items)

May (2004)

(15 items)

April (2004)

(15 items)

March (2004)

(13 items)

February (2004)

(16 items)

January (2004)

(15 items)

Blog Home

RSS 2.0

Writing

Programming C# 5.0

Programming WPF

Other Sites

Interact Software

What Exactly Is an Unmanaged Resource?

Thursday 3 June, 2004, 04:45 PM

Given the distinction .NET draws between managed and unmanaged code, it is tempting to think that unmanaged resources are the kinds of things you tend to deal with in unmanaged code. You know the kinds of things - handles, raw pointers to blocks in the unmanaged C++ heap, sockets, and so on.

But if you think about it, that's a bizarrely specific definition. I prefer this much simpler one:

Unmanaged resources are those resources that the CLR doesn't manage for you.

This obviously includes all the items listed above, but is a little more open ended. In particular, it doesn't necessarily preclude those resources that were acquired using a pure .NET API, and which don't have a directly tangible manifestation such as a handle or file. For example, does the CLR manage lock ownership?

Why does this matter? Well this came up due to a discussion over the use of the IDisposable interface, which is documented thus:

Defines a method to release allocated unmanaged resources.

John Elliot evidently thinks I am wrong to use the C# using keyword to manage lock acuisition. (You knew I wasn't going to manage going a whole month without mentioning it...) He says:

My point is that if you use 'using' to call Dispose for some other purpose (like Ian's TimedLock that has been floating around just to infuriate me for the last couple of weeks) then you're doing the wrong thing. You are abusing a system interface because you like the facility that you can get from it.

His opinion here is strikingly different from what a PM on the C# team at Microsoft had to say about me using the using statement in this way:

we decided to name it "using" rather than something more specific to disposing objects so that it could be used for exactly this scenario.

:-)

If you use the first, rather narrow definition of an unmanaged resource, then arguably John is right. But if you use the second, more straightforward definition, then it's clear that locks are unmanaged resources. The CLR just gives me a couple of low level primitives: Enter and Leave. These are to locking what malloc and free (or if you prefer, new and delete) are to memory. Of course memory is one of the things that the CLR does manage, so we are no longer required to free the memory we allocated.

Locks, however, are entirely our problem - just as much as memory management used to be in the pre-.NET days.

So I think that using and IDisposable are appropriate for scope-based management of any resource that is not managed for you by the CLR. And that emphatically includes lock ownership.

[Update: Philip Haack emailed me, partly to take me to task for not having implemented comments in my blog yet, but mainly to point out an interesting conversation he had with Eric Gunnerson on this subject. It's about half way down, in the paragraph starting "I asked Eric Gunnerson...". Apparently the lock keyword probably wouldn't even have made it into the language if using had been invented earlier, because you can recreate lock-like semantics in the way that my TimedLock does.]

Copyright © 2002-2018, Interact Software Ltd. Content by Ian Griffiths. Please direct all Web site inquiries to webmaster@interact-sw.co.uk