Welcome to Manicprogrammer Sign in | Join | Help

Rediscovering the Obvious

An occasional journey through one man's perspectives as he fumbles along in the footsteps of many great men.
IDisposable.Dispose isn't.

Ok - Rule #1 of .NET programming: If it's Disposable, dispose it... wrap it in a using statement, call Dispose... SOMETHING!

Easy, once you think that way, nothing to worry about.

Except... when you declare a Mutex, try to dispose it, and get this message:

Error    'System.Threading.WaitHandle.Dispose(bool)' is inaccessible due to its protection level

Now what? looking at the docs: http://msdn2.microsoft.com/en-us/library/system.threading.mutex_methods.aspx shows that the method is indeed listed as protected. I was unaware that you COULD protect an interface implementation... that aside, it seems that it's even worse that I'm not able to call Dispose on something.

Now that I've shared my confusion with the world, can anybody help me figure out what's going on? If I had more time and energy, I'd pull up reflector, dig out the C# language and .NET runtime specs, and see what I could find. Tonight, however, I'll leave this as an exercise for my two readers to figure out.

Published Wednesday, January 09, 2008 10:17 PM by willeke

Filed under:

Comments

# re: IDisposable.Dispose isn't. @ Thursday, January 10, 2008 9:00 AM

Have you tried calling Dispose without args? I think that's the method that's defined by the interface. Dispose(bool) is (or was, as of .NET 2) a convention, IIRC.

spraints

# re: IDisposable.Dispose isn't. @ Thursday, January 10, 2008 9:09 AM

The Dispose() is also protected, but as your suggestion made me realize, undocumented on MSDN, only the parameterized version is documented.

HELP!

willeke

# re: IDisposable.Dispose isn't. @ Thursday, January 10, 2008 9:50 AM

Try casting to IDisposable, or using a using block. That makes the compiler and runtime ignore (or not know) that the implementation made it protected.

spraints

# re: IDisposable.Dispose isn't. @ Thursday, January 10, 2008 10:02 AM

 finally

 {

   if ( mutex != null )

   {

     ((IDisposable) mutex).Dispose();

   }

 }

This works, or at least, compiles fine and doesn't blow up.

willeke

# re: IDisposable.Dispose isn't. @ Monday, January 14, 2008 10:41 AM

Heya, guys.

My gut reaction is that casting away the protection level is probably a bad idea, and that it works by coincidence.  And it might be that way just so you can use a using block.

But, I am betting the public interface doesn't contain an oversight.

In an MSDN article on implementing finalize and dispose (http://msdn2.microsoft.com/en-us/library/b1yfkh5e.aspx):

"Customizing a Dispose Method Name

Occasionally a domain-specific name is more appropriate than Dispose. For example, a file encapsulation might want to use the method name Close. In this case, implement Dispose privately and create a public Close method that calls Dispose."

The article says that implementing explicit disposal is optional.  And whether or not Mutex is "renaming Dispose", there's a case for it having its own method of resource management.  Sure enough, the public interface contains two likely candidate: Close and ReleaseMutex.

Really, what we care about in disposing is making sure that our discarded Mutex object doesn't block subsequent access to code.  Those sound like reasonable method names for this.  And this Dr. Dobbs article (http://www.ddj.com/windows/184416856) (among others) list using the mutex for what we were attempting, /without/ disposing.

Odds are, disposing is misuse here? Thoughts?

mbarrington

# Dan Rigsby - Coding Up Style » Blog Archive » Don’t Wrap Wcf Service Hosts or Clients in a Using Statement @ Tuesday, February 26, 2008 11:09 AM

PingBack from http://www.danrigsby.com/blog/index.php/2008/02/26/dont-wrap-wcf-service-hosts-or-clients-in-a-using-statement/

Dan Rigsby - Coding Up Style » Blog Archive » Don’t Wrap Wcf Service Hosts or Clients in a Using Statement

Anonymous comments are disabled