Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

GC.ReRegisterForFinalize Method

Requests that the system call the finalizer method for the specified object, for which SuppressFinalize has previously been called.

[Visual Basic]
Public Shared Sub ReRegisterForFinalize( _
   ByVal obj As Object _
)
[C#]
public static void ReRegisterForFinalize(
 object obj
);
[C++]
public: static void ReRegisterForFinalize(
 Object* obj
);
[JScript]
public static function ReRegisterForFinalize(
   obj : Object
);

Parameters

obj
The object for which a finalizer must be called.

Exceptions

Exception Type Condition
ArgumentNullException obj is a null reference (Nothing in Visual Basic).

Remarks

The ReRegisterForFinalize method adds obj to the list of objects that request finalization before the garbage collector frees the object. The obj parameter must be the caller of this method.

Calling the ReRegisterForFinalize method does not guarantee that the garbage collector will call an object's finalizer.

By default, all objects that implement finalizers are added to the list of objects that require finalization; however, an object might have already been finalized, or might have disabled finalization by calling the SuppressFinalize method.

A finalizer can use this method to resurrect itself or an object it references.

Example

[Visual Basic] 
Imports System

Namespace ReRegisterForFinalizeExample
    Class MyMainClass
        Shared Sub Main()
            'Create a MyFinalizeObject.
            Dim mfo As New MyFinalizeObject()

            'Release the reference to mfo.
            mfo = Nothing

            'Force a garbage collection.
            GC.Collect()

            'At this point mfo will have gone through the first Finalize.
            'There should now be a reference to mfo in the static
            'MyFinalizeObject.currentInstance field.  Setting this value
            'to null and forcing another garbage collection will now
            'cause the object to Finalize permanently.
            MyFinalizeObject.currentInstance = Nothing
            GC.Collect()
        End Sub
    End Class

    Class MyFinalizeObject
        Public Shared currentInstance As MyFinalizeObject = Nothing
        Private hasFinalized As Boolean = False

        Protected Overrides Sub Finalize()
            If hasFinalized = False Then
                Console.WriteLine("First finalization")

                'Put this object back into a root by creating
                'a reference to it.
                MyFinalizeObject.currentInstance = Me

                'Indicate that this instance has finalized once.
                hasFinalized = True

                'Place a reference to this object back in the
                'finalization queue.
                GC.ReRegisterForFinalize(Me)
            Else
                Console.WriteLine("Second finalization")
            End If
            MyBase.Finalize()
        End Sub
    End Class
End Namespace

[C#] 
using System;

namespace ReRegisterForFinalizeExample
{
    class MyMainClass
    {
        static void Main()
        {
            // Create a MyFinalizeObject.
            MyFinalizeObject mfo = new MyFinalizeObject();

            // Release the reference to mfo.
            mfo = null;

            // Force a garbage collection.
            GC.Collect();

            // At this point mfo will have gone through the first Finalize.
            // There should now be a reference to mfo in the static
            // MyFinalizeObject.currentInstance field.  Setting this value
            // to null and forcing another garbage collection will now
            // cause the object to Finalize permanently.
            MyFinalizeObject.currentInstance = null;
            GC.Collect();
        }
    }

    class MyFinalizeObject
    {
        public static MyFinalizeObject currentInstance = null;
        private bool hasFinalized = false;

        ~MyFinalizeObject()
        {
            if(hasFinalized == false)
            {
                Console.WriteLine("First finalization");
            
                // Put this object back into a root by creating
                // a reference to it.
                MyFinalizeObject.currentInstance = this;
            
                // Indicate that this instance has finalized once.
                hasFinalized = true;

                // Place a reference to this object back in the
                // finalization queue.
                GC.ReRegisterForFinalize(this);
            }
            else
            {
                Console.WriteLine("Second finalization");
            }
        }
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;

__gc class MyFinalizeObject {
public:
   static MyFinalizeObject* currentInstance = 0;
private:
   bool  hasFinalized;
public:
   MyFinalizeObject() {
      hasFinalized = false;
   }
   ~MyFinalizeObject() {
      if (hasFinalized == false) {
         Console::WriteLine(S"First finalization");

         // Put this object back into a root by creating
         // a reference to it.
         MyFinalizeObject::currentInstance = this;

         // Indicate that this instance has finalized once.
         hasFinalized = true;

         // Place a reference to this object back in the
         // finalization queue.
         GC::ReRegisterForFinalize(this);
      } else {
         Console::WriteLine(S"Second finalization");
      }
   }
};

int main() {
   // Create a MyFinalizeObject.
   MyFinalizeObject* mfo = new MyFinalizeObject();

   // Release the reference to mfo.
   mfo = 0;

   // Force a garbage collection.
   GC::Collect();

   // At this point mfo will have gone through the first Finalize.
   // There should now be a reference to mfo in the static
   // MyFinalizeObject::currentInstance field.  Setting this value
   // to 0 and forcing another garbage collection will now
   // cause the object to Finalize permanently.
   MyFinalizeObject::currentInstance = 0;
   GC::Collect();
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

GC Class | GC Members | System Namespace | SuppressFinalize

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.