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 7967373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:40:45+00:00 2026-06-04T06:40:45+00:00

I have test code like this: public class A : CriticalFinalizerObject { ~A() {

  • 0

I have test code like this:

public class A : CriticalFinalizerObject 
{
    ~A()
    {
        File.WriteAllText("c:\\1.txt", "1z1z1");
    }
}

class Program
{
    static void Main(string[] args)
    {
        A a = new A();
        throw new Exception();
    }
}

First I tried running it without deriving A from CriticalFinalizerObject. Finalizer wasn’t called after end of this program.
That surprised me as I thought it was more deterministic but okay. Then I’ve read about CriticalFinalizerObject’s that ensure their finalizers will be called. I derived A from it.
Guess what. It still doesn’t get executed.
What am I doing/understanding wrong?

(Please don’t write obvious stuff about garbage collector being non-deterministic, I know that. It is not the case as the program is over and I imagined I could safely clean up after a nice unhandled managed exception.)

  • 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-04T06:40:47+00:00Added an answer on June 4, 2026 at 6:40 am

    Firstly, let’s read about CriticalFinalizerObject in MSDN, we can read, that:

    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.

    The main word here is UNLOAD.

    Secondly, let’s read MSDN again, this time about Exceptions in managed threads:

    If these exceptions are unhandled in the main thread, or in threads that entered the runtime from unmanaged code, they proceed normally, resulting in termination of the application.

    The main word is TERMINATION.

    So, when there is an unhandled exception in main thread – app terminates, but CriticalFinalizerObject helps only on unloading of Domain.

    For example, CriticalFinalizerObject can helps in such situation:

    // Create an Application Domain:
    AppDomain newDomain = AppDomain.CreateDomain("NewApplicationDomain");
    
    // Load and execute an assembly:
    newDomain.ExecuteAssembly(@"YouNetApp.exe");
    
    //Unload of loaded domain
    AppDomain.Unload(newDomain);
    

    This is a situation, where domain was unloaded, and CriticalFinalizerObject guarantee you, that your finalizer will be called.

    In your situation with terminating of app you can try to subscribe to

    AppDomain.CurrentDomain.UnhandledException
    

    and manually finalize your objects.

    UPD:
    Jeffrey Richter in his book “CLR via C#” wrote about CriticalFinalizerObject, that it’s for situations where you send your code for example to SQLServer, which can run C# as a procedures. In such case CriticalFinalizerObject helps you to clean your object, if SQLServer will unload your library’s Domain.
    Also CriticalFinalizerObject is for situations where you need in finalizer of object to call method of another object, because of CriticalFinalizerObject guarantee you, that it’s finalizer will be called after finalizers of all non CriticalFinalizerObject objects.

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

Sidebar

Related Questions

I have code like this: public class test { public string aa { get;
I have code like this: public class DatabaseHelper extends SQLiteOpenHelper { private static final
Suppose I have a Java class like this: public class Test { static {
Hi all I have a piece of code which looks like this: public class
I have code a bit like this public class MyObject { private bool IsValidDay(ref
If I have an inner class, like this: public class Test { public class
i have code like this public class People { public string name { get;
I have code like this: string uriString = @C:\Temp\test.html; Uri uri = new Uri(uriString);
Say I have a test like: void TestSomething(int someParam) { // Test code }
I have this test code that just saves an XML file to a folder.

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.