Why does AppDomain.Unload() error in finalizer?
Asked Answered
B

1

2

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.)

Bonnybonnyclabber answered 11/12, 2009 at 23:9 Comment(0)
S
3

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.

Spectre answered 11/12, 2009 at 23:29 Comment(4)
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.Bonnybonnyclabber
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?Debauch
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.Bonnybonnyclabber
I expected the GC to do clean up as well but actually testing this out doesn't ever see the GC clean up AppDomains on it's own, even when calling GC.Collect explicitly, which does trigger other finalizers to run.Stimulus

© 2022 - 2024 — McMap. All rights reserved.