6

I'm using the pattern described here to manage disposals: http://www.developerzen.com/2006/01/09/finalizableobject-developing-a-base-class-for-idisposable-objects/

What counts as an "Unmanaged Resource?"

Here are some of the confusing points in my mind:

  • At some point, .NET is just wrapping Win32 calls, right? So aren't most of the .NET objects in a way an Unmanaged Resource?
  • What about COM objects that we have .NET wrappers for -- what are they considered?
  • What about managed classes that get functionality solely from P/Invokes?
  • What about C++/CLI classes that internally use native libraries? At the C# level, the C++/CLI classes that had destructors now implement IDisposable... what are they considered?

I realize that blog post is a bit old. If there's a more modern way of managing the lifetime of unmanaged and managed objects please suggest it.

EDIT: Whoever is voting to close this, please provide some details on how I can improve the question.

11
  • "If there's a newer way of addressing this problem..." What, exactly, is the problem here? Mar 30, 2012 at 19:38
  • Manging the lifecycle of unmanaged and managed objects. If you do this in the wrong order you can have deadlocks
    – jglouie
    Mar 30, 2012 at 19:40
  • What's wrong with the example given in the documentation? Mar 30, 2012 at 19:41
  • 2
    @CodyGray, what's your problem with question? He is asking clarifications out of his thoughts.
    – Akash Kava
    Mar 30, 2012 at 19:41
  • @Akash: I was asking for a clarification also. I didn't understand what the problem he was trying to solve was. When did I say or indicate that I had a problem with the question? Mar 30, 2012 at 19:42

3 Answers 3

6

I understand the confusion. I think there are a few simple guidelines to follow:

  1. If your class "owns" a reference to another object (i.e. your object created the other object, or was given the reference to that object with the understanding that your object now "owns" it), then you are responsible for doing any necessary cleanup (if any)

  2. If that other object is a .NET object and does not implement the IDisposable interface, then you probably don't need to do anything. The IDisposable interface is, by convention, the interface that we use to declare whether or not our objects require cleanup.

  3. If that other object is a .NET object and implements the IDisposable interface, then your class should also implement the IDisposable interface. In your Dispose method, just call the Dispose method of the other object.

  4. If that other object is an unmanaged object or resource, then your class should implement a finalizer to ensure that the unmanaged object / resource will get cleaned up. Follow the guidelines described in that blog post for how to implement that.

Below are some specific answers to your questions.

  • At some point, .NET is just wrapping Win32 calls, right? So aren't most of the .NET objects in a way an Unmanaged Resource?

Even though a .NET object may wrap Win32 calls, the term "Unmanaged Resource" is not appropriate because it is a .NET object, therefore it is by definition a "Managed Resource". The CLR handles the memory allocation and garbage collection for those objects.

  • What about COM objects that we have .NET wrappers for -- what are they considered?

.NET wrapper objects are still .NET objects, so they are "managed resources", but note that they must implement their own dispose/finalize logic. Basically, they take care of cleaning up the "unmanaged resources" so that you don't have to. If you were implementing your own wrapper for a COM object, then you would be responsible for implementing the necessary dispose/finalize logic to clean it up, so that the consumers of your wrapper don't have to.

  • What about managed classes that get functionality solely from P/Invokes?

A managed class that uses P/Invoke to call unmanaged code might be allocating unmanaged resources, depending on what it is calling. Therefore, it may be necessary for it to implement the necessary dispose/finalize logic to clean up those unmanaged resources.

  • What about C++/CLI classes that internally use native libraries?

Yes, those also may be allocating unmanaged resources from the native libraries, so it would also be necessary for them to implement the necessary destructor/finalizer logic.

  • At the C# level, the C++/CLI classes that had destructors now implement IDisposable... what are they considered?

In C++/CLI, you can choose to declare a class as either managed (with the ref keyword) or unmanaged (without the ref keyword). If it's declared with the ref keyword, then it is a managed class; it will be allocated on the managed heap and will be cleaned up by the garbage collector once it falls out of scope and there are no more references to it. If the class is declared without the ref keyword, then it is unmanaged and must be explicitly cleaned up. In either case, the class might have allocated unmanaged resources that need to be cleaned up in the destructor/finalizer.

1
  • Thank you very much for addressing the bullets -- this really clears up the concepts for me.
    – jglouie
    Mar 30, 2012 at 20:21
1

Here is what I treat as "unmanaged" (note the quotes):

  1. Anything that implements IDisposable. If it wants to be disposed ... well, trust it. It doesn't matter if it is pure "safe" .NET or implements an empty Dispose method. For the sake of handling it with the Disposable pattern it is "unmanaged".
  2. Any COM RCW. There are huge debates on this; I view each COM->NET RCW count-increase as "owned" and thus "unmanaged". Ultimately a RCW will clean itself up in the destructor, but "ultimately" can cause lots of issues when dealing with the Outlook Object Model, which is what I work with. This approach requires a consistent approach to dealing with COM objects and lifetimes to avoid "RCW separated" issues.
  3. Any [new] resource obtained through a P/Invoke call such as a BSTR: the thing that allocates said resource "owns" it until control is relinquished. This really is an "unmanaged" resource and must be released. There are some helpful wrapper types for the various resources that can be allocated.

I don't deal with non-COM native code (e.g. CLI), excepting a few WinAPI calls, which are all C.

0

On first thought, anything that is outside .net is unmanaged resource. Managed objects are .net strings, arrays, value types and complex types that are made up of them, all of them are managed and their life cycle is automatically controlled by .net. String is not a wrapper around something so it is completely managed.

Most .net base classes are written from scratch, like List, Stack etc. we can simplify by saying that any object and it's descendants are only referencing data within your stack or heap is managed.

Any other opened connection/handle to access anything outside your stack/heap is unmanaged, be it file, named pipe, network or any device.

Most other BCL classes are optimized but unless you wrap them around using block, they will not be disposed automatically in whichever way they are written.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.