locked
Calling .NET runtime statics in Finalizer during App Domain unload RRS feed

  • Question

  • Hi,

    I was wondering if it's safe to call static methods belonging to .NET runtime classes, such as, System.Runtime.InteropSerices.Marshal.ReadIntPtr, for example, in Finalizer during AppDomain unload ( i.e. System.Environment.HasShutdownStarted is true ).  I know that accessing other custom static classes is not safe, since they may already be unloaded, but I couldn't find any information whether the same is true for .NET runtime classes.

    Thanks,

    Alex

    Friday, January 13, 2012 5:31 PM

Answers

  • I'm pretty sure you are not creating any classes (such as Marshal in your example). As far as I know, Marshal is just a container for a bunch of static *methods* that are perfectly safe to call from a finalizer. You're not doing a *new* of anything.


    Phil Wilson
    • Marked as answer by Paul Zhou Monday, January 23, 2012 8:12 AM
    Tuesday, January 17, 2012 8:49 PM
  • Finalizer is always used to release native resources, it is a special method but is also a common method. Even some objects have been created, after the method executed, when GC examines that the objects are unreachable, they would be collected. If some native resources are opened in the method, remember that Base.Finalize method would be called in finally block. So native resource would be released too.


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    • Marked as answer by Paul Zhou Monday, January 23, 2012 8:12 AM
    Wednesday, January 18, 2012 6:04 AM

All replies

  • Hi Alex,

    .NET runtime classes have their specified actions. I do not understand how you can "call static methods belonging to .NET runtime classes in Finalizer during AppDomain unload"?

    Actually, in Finalizer, it is used to release native resources.

    Thanks.


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Monday, January 16, 2012 8:29 AM
  • Hi Paul,

    in finalizer, for example, I can call things like Marshal.ReadIntPtr/WriteIntPtr, which are static methods on a static class Marshal.  What I am wondering is how safe are those calls in my finalizer if it's being called as a result of AppDomain being unloaded.  I know from experience that calling custome static classes, that I've created, in the finalizer while AppDomain is being unloaded can result in ObjectDisposedEcxeption.  What I would like to know if the same is possible for the classes that are part of .NET runtime, such as Marshal.

    Thanks,

    Alex

    Monday, January 16, 2012 8:45 PM
  • Hi Alex,

    Thanks for your feedback.

     

    Actually, if you create a class that implements Finalizer method by yourself in C#, you must define a Finalize method by placing a tilde symbol (~) in front of the class name  as below:

     internal sealed class SomeType
        {
            //This is the Finalize method
            ~SomeType(){
             //The code here is inside the Finalize method
            }
        }
    

    If you were to compile this code and examine the resulting assembly with ILDasm.exe, you'd see that the C# compiler did, in fact, emit a protected override method named Finalize into the module's metadata. If you examined the Finalize method's IL code, you'd also see that the code inside the method's body is emitted into a try block, and that a call to base.

    Finalize is emitted into a finally block. So if any exception threw, it would be catch and handle.

    A Finalize method is usually implemented to call the Win32 CloseHandle function, passing in the handle of the native resource and release it.


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Tuesday, January 17, 2012 8:46 AM
  • Hi Paul,

    yes, I am aware of how the compiler represents the finalizer internally.  However, as you've mentioned, there's no catch() block, as far as I know, in that internal representation.  I.e., it's:

    try 
    {
    // finalizer code
    }
    finally
    {
    }
    


    Thus, exceptions thrown inside finalizer code are not actually processed.  However, as I mentioned, what I am trying to find out is if classes like Marshal are also being unloaded when AppDomain is being unloaded, and, thus, can be disposed BEFORE my class gets disposed, in which case, accessing even Marshal class can throw an exception.

    Tuesday, January 17, 2012 2:38 PM
  • I'm pretty sure you are not creating any classes (such as Marshal in your example). As far as I know, Marshal is just a container for a bunch of static *methods* that are perfectly safe to call from a finalizer. You're not doing a *new* of anything.


    Phil Wilson
    • Marked as answer by Paul Zhou Monday, January 23, 2012 8:12 AM
    Tuesday, January 17, 2012 8:49 PM
  • Finalizer is always used to release native resources, it is a special method but is also a common method. Even some objects have been created, after the method executed, when GC examines that the objects are unreachable, they would be collected. If some native resources are opened in the method, remember that Base.Finalize method would be called in finally block. So native resource would be released too.


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    • Marked as answer by Paul Zhou Monday, January 23, 2012 8:12 AM
    Wednesday, January 18, 2012 6:04 AM