Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7784313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:04:31+00:00 2026-06-01T20:04:31+00:00

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

  • 0
 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();
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T20:04:32+00:00Added an answer on June 1, 2026 at 8:04 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

class Program { static void Main(string[] args) { List<Book> books = new List<Book> {
I have the following code: class Program { static void Main(string[] args) { new
class Program { static void Main(string[] args) { var x = new Program(); Console.Write(x.Text);
class Program { static void Main(string[] args) { var dictionary = new Dictionary<string, int>()
class Program { static void Main(string[] args) { var getfiles = new fileshare.Program(); string
class Program { static void Main(string[] args) { List<A> myList = new List<A> {new
class Program { static void Main(string[] args) { baseClass obj = new baseClass(); obj.intF
class Program { static void Main(string[] args) { Func<Object> someMethod = new Func<Object>(((Object)null).ToString); String
If I write this: class Program { static void Main(string[] args) { throw new
class Program { static void Main(string[] args) { MyDatabaseEntities entities = new MyDatabaseEntities(); var

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.