views:

173

answers:

1

Here's some sample code:

using System;
namespace UnloadFromFinalizer
{
    class Program
    {
        static void Main(string[] args)
        {
            Program p = new Program();
        }

        AppDomain domain;
        Program()
        {
            this.domain = AppDomain.CreateDomain("MyDomain");
        }

        ~Program()
        {
            AppDomain.Unload(this.domain);//<-- Exception thrown here
        }
    }
}

I have a class that creates an AppDomain in the constructor to be used over the lifetime of the object. I'd like to properly cleanup the AppDomain, so I thought I would call Unload in the finalizer. Unfortunately, that causes a CannotUnloadAppDomainException to be thrown. The MSDN documentation for AppDomain.Unload notes:

In some cases, calling Unload causes an immediate CannotUnloadAppDomainException, for ample if it is called in a finalizer.

Why is this? Is the member variable "domain" already cleaned up? Does that cleanup automatically include unloading the AppDomain, or will it still exist in some unreachable way? Is there something I should be doing, or can I safely just dump the finalizer? (I don't really care when the GC gets rid of my object so long as it's fully cleaned up in the process.)

+3  A: 

The AppDomain class does not have a finalizer defined and so will just be garbage collected as normal. The finalizer of your Program class will be called from the finalizer thread of the garbage collector. When this happens, there is no guarantee that your AppDomain instance will or will not have been garbage collected yet and so you will get undetermined behaviour.

I would not bother with the finalizer of Program, as the AppDomain will get garbage collected anyway, plus in addition, the whole process will be destroyed when the Main method exits anyway.

adrianbanks
This is just a sample program. In my real application, my object doesn't live in Main and could get GCed before my app is finished. I guess I just wanted to ensure that the GC would clean up the AppDomain correctly on it's own. It sounds like it will.
Nogwater
More generally, the pattern of finalizing things which are already managed objects seems suspicious to me. I normally think of a finalizer as something that closes a kernel handle, or some such thing. Do you habitually finalize managed objects?
Eric Lippert
Nope. This was actually my first time trying out finalizers. I didn't know that the AppDomain would be fully managed. I guess if it wasn't it would implement IDisposable.
Nogwater