Eliminate an IDisposable stored in an open static field

If the class has an instance field that implements IDisposable, then the containing class implements IDisposable and the class that processes the Dispose method from its Dispose method.

public class A : IDisposable { public System.Drawing.Font font = new Font("Arial", 10.0f); public void Dispose() { font.Dispose() } } 

(I know that I did not install the template correctly, but for the sample code should be enough)

If the field is a static field, then where should the call in the Dispose field be?

 public class B { public static System.Drawing.Font font = new Font("Arial", 10.0f); } 

I could make class B an IDisposable implementation and have this call to font.Dispose , but if later, B.font will be used later, it will cause problems. In addition, you will need to remember that when accessing the static method, you need to create an instance to call Dispose.

I could also create a static Dispose method, but then users must remember that you are calling Dispose and must make sure that they are the last user of this in the program.

+3
source share
4 answers

Static fields are initialized when the type is loaded.

Therefore, it logically makes sense to dispose of the object assigned to the static field when unloading the containing type.

However, types are not unloaded. There might be some exotic complication around AppDomains, but I suspect this is not applicable in your case.

Therefore, I would not choose an instance, otherwise you will have a public instance of the object that is unusable.

+8
source

If the field is static, perhaps the intention is for it to last the entire duration of the application? Because then it will need to be disposed of only when the application is closed. And this will happen one way or another, in and of itself -

If you plan to reassign the static field to various IDisposable objects several times during the lifetime of the application, then, of course, you will want to get rid of the old object when reassigning. Maybe you can use a property for this? Not that I thought much about it, but something like:

 // private - don't write to this field from outside the property setter static Font font = new Font("Arial", 10.0f)); public static Font Font { get { return font; } set { var oldFont = font; if (oldFont != null) oldFont.Dispose(); font = value; } } 
+3
source

You dispose of it like any other object. It does not matter. A static object is just an object available for each class. It is still something. Of course, you probably will not want to do this, because after you get rid of it, someone will still be able to access it and get an ObjectDisposedException .

  static void Main(string[] args) { using (Test.Instance) { } Thread.Sleep(TimeSpan.FromSeconds(10)); } public class Test:IDisposable { public static Test Instance = new Test(); public void Dispose() { Console.WriteLine("Disposed"); } } 

And the result:

Disposed

In the updated example:

 public class A { public static System.Drawing.Font font = new Font("Arial", 10.0f)); } 

You can simply do A.font.Dispose() or using(A.font)

In general, I think the idea is that you really do not have a public static one-time field, because by setting it to static, you mean that it should be for the whole life of the application. If it should be disposable, you should make it lazy and make it safe for reinitialization by the stream, otherwise access to it after disposal will throw an exception. Or you have a hook at the end of the application code and safely dispose of all static disposable items there. You can also register your one-time starter. Just an idea

+2
source

You cannot destroy a static class because there is no instance of it

-1
source

Source: https://habr.com/ru/post/1210117/


All Articles