Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How and when are c# Static members disposed?

I have a class with extensive static members, some of which keep references to managed and unmanaged objects.

For instance, the static constructor is called as soon as the Type is referenced, which causes my class to spin up a blockingQueue of Tasks. This happens when one of the static methods is called, for example.

I implemented IDisposable, which gives me methods to handle disposal on any instance objects I created. However, these methods are never called if the consumer doesn't create any instance objects from my class.

How and where do I put code to dispose of references maintained by the static portion of my class? I always thought that disposal of static-referenced resources happened when the last instance object was released; this is the first time I've ever created a class where no instances may ever be created.

like image 369
Joe Avatar asked Aug 25 '12 23:08

Joe


People also ask

Does your cervix open during C-section?

During elective (planned) caesarean sections, some obstetricians routinely dilate the cervix intraoperatively, using sponge forceps, a finger, or other instruments, because the cervix of women not in labour may not be dilated, and this may cause obstruction of blood or lochia drainage.

Are you awake during C?

Most C-sections are done under regional anesthesia, which numbs only the lower part of your body. This allows you to be awake during the procedure. Common choices include a spinal block and an epidural block. Some C-sections might require general anesthesia.

How long is too long for labor?

If your baby is not born after approximately 20 hours of regular contractions, you are likely to be in prolonged labor. Some health experts may say it occurs after 18 to 24 hours. If you are carrying twins or more, prolonged labor is labor that lasts more than 16 hours.

Are C-sections painful?

You won't feel any pain during the C-section, although you may feel sensations like pulling and pressure. Most women are awake and simply numbed from the waist down using regional anesthesia (an epidural and/or a spinal block) during a C-section. That way, they are awake to see and hear their baby being born.


2 Answers

The static variable of your class are not garbage collected until the app domain hosting your class is unloaded. The Dispose() method will not be called, because it is an instance method, and you said that you wouldn't create any instances of your class.

If you would like to make use of the Dispose() method, make your object a singleton, create one instance of it, and dispose of it explicitly when your application is about to exit.

public class MyClass : IDisposable {     public IList List1 {get; private set;}     public IDictionary<string,string> Dict1 {get; private set;}     public void Dispose() {         // Do something here     }     public static MyClass Instance {get; private set;}     static MyClass() {         Instance = new MyClass();     }     public static void DisposeInstance() {         if (Instance != null) {             Instance.Dispose();             Instance = null;         }     } } 
like image 77
Sergey Kalinichenko Avatar answered Sep 22 '22 19:09

Sergey Kalinichenko


public class Logger : IDisposable {      private string _logDirectory = null;     private static Logger _instance = null;      private Logger() : this(ConfigurationManager.AppSettings["LogDirectory"])     {         AppDomain.CurrentDomain.ProcessExit += CurrentDomain_ProcessExit;     }      private Logger(string logDirectory)      {     }       public static Logger Instance     {         get         {             if (_instance == null)                 _instance = new Logger();             return _instance;         }     }      private void CurrentDomain_ProcessExit(object sender, EventArgs e)     {         Dispose();     }        public void Dispose()     {         // Dispose unmanaged resources     } } 
like image 36
Guido Kleijer Avatar answered Sep 23 '22 19:09

Guido Kleijer