Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why my Close function isn't called?

 class Program : CriticalFinalizerObject
    {
        static void Main(string[] args)
        {

            Program p = new Program();
            TextWriterTraceListener listener = new TextWriterTraceListener(@"C:\trace.txt");
            Trace.Listeners.Clear(); // Remove default trace listener
            Trace.Listeners.Add(listener);
            Trace.WriteLine("First Trace"); // Generate some trace messages
            Trace.WriteLine("Perhaps last Trace.");

        }

        ~Program()
        {
            Trace.Close();
        }
    }

I get file size =0

the finilizer should have executed because I derive from CriticalFinalizerObject

I don't want to use Trace.Close() not in the Finalizer.

edit

after @eric Lippert reply : Ive re-edited the code trying to match it to :constrained execution region ( but still no success)

  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
    class Program : CriticalFinalizerObject
    {

        static void Main(string[] args)
        {
            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
            }
            catch (Exception e)
            {
            }
            finally
            {
                Program p = new Program();
                TextWriterTraceListener listener = new TextWriterTraceListener(@"C:\trace1.txt");
                Trace.Listeners.Clear();
                Trace.Listeners.Add(listener);
                Trace.WriteLine("First Trace");
                Trace.WriteLine("Perhaps last Trace.");
            }
        }

        ~Program()
        {
            Trace.Flush();
        }
    }
like image 971
Royi Namir Avatar asked Apr 06 '12 13:04

Royi Namir


2 Answers

As the documentation clearly states:

In classes derived from the CriticalFinalizerObject class, the common language runtime (CLR) guarantees that all critical finalization code will be given the opportunity to execute, provided the finalizer follows the rules for a CER, even in situations where the CLR forcibly unloads an application domain or aborts a thread. If a finalizer violates the rules for a CER, it might not successfully execute

Does your finalizer follow all the rules for a constrained execution region?

UPDATE:

You've updated your code in an attempt to make it follow the rules of constrained execution regions, but I see no evidence whatsoever that you've done so correctly. The rules are quite clear; a finalizer in a constrained execution region absolutely must not do any of these things:

  • allocate memory
  • box a value type
  • acquire a lock
  • call an arbitrary virtual method
  • call any method which lacks a reliability contract

Does your finalizer do any of those five things? If it does then there is no requirement that the CLR honours your desire for the finalizer to always run.

Moreover: forget about constrained execution regions for a moment because your program as it stands now is not even threadsafe. You've written a nasty race condition into the program.

What stops the jitter from garbage-collecting p before the trace starts? Nothing! The jitter knows that p will never be used again, and is perfectly within its rights to collect it immediately after its allocation. The flush could be happening at any time on the finalizer thread, including before the trace writes happen, or in the middle of any of them.

like image 52
Eric Lippert Avatar answered Nov 09 '22 10:11

Eric Lippert


Because you didn't create instance of Program class.

You can read more here:

This method is automatically called after an object becomes inaccessible, unless the object has been exempted from finalization by a call to SuppressFinalize. During shutdown of an application domain, Finalize is automatically called on objects that are not exempt from finalization, even those that are still accessible. Finalize is automatically called only once on a given instance, unless the object is re-registered using a mechanism such as ReRegisterForFinalize and GC.SuppressFinalize has not been subsequently called.

So, you need to have instance of object, if you want finilizer to be called.

UPDATE: Consider using Trace.AutoFlush = true; if you want message to be written.

UPDATE: (Why Close function isn't called?) Actually you Close function is called (if nothing exceptional happens in other finalizers). If you will keep default TraceListener (remove Trace.Listeners.Clear() call), you'll see that all strings successfully written to Output window.

Problem here is that StreamWriter (which is created inside TextWriterTraceListener) does not has finalizer. So it does not flush all data to file. What you need to do:

FileStream file = new FileStream(@"C:\trace.txt", FileMode.OpenOrCreate);
StreamWriter writer = new StreamWriter(file);
GC.SuppressFinalize(file);
GC.SuppressFinalize(file.SafeFileHandle);
var listener = new TextWriterTraceListener(writer);

Actually you will need to close file manually on your finalizer.

like image 36
Sergey Berezovskiy Avatar answered Nov 09 '22 12:11

Sergey Berezovskiy