Singleton having finalizer without IDisposable

0 votes

Here's what I understand about IDisposable and finalizers from "CLR via C#", "Effective C#" and other resources:

  • IDisposable can clean up managed and unmanaged resources deterministically.
  • Classes responsible for unmanaged resources(e.g. file handles) generally implement IDisposable and provide a finalizer to guarantee their clean up even if client codes did not call Dispose() on the instance.
  • Classes responsible for only managed resources must not implement a finalizer.
  • If you have a finalizer then you must implement IDisposable (so that client codes can also call Dispose(), while the finalizer is just to prevent leaking resources in case they forget).

Now, I can understand the reasons behind each of the above, but then I also believe there could be a scenario where breaking these rules might make more sense. Like, say, a singleton class that is responsible for unmanaged resources (such as in cases of providing a single point of access to particular files). I think it'll be wrong to always have a method Dispose() on a singleton because that singleton instance must live for the life of the application and if any client code calls Dispose() then that does it and you are stuffed. However, you'd still want a finalizer that will clean up unmanaged resources when the application is unloaded. Thus, a singleton class with a finalizer that does not implement IDisposable seems more ideal and reasonable to me. Yet, this design completely goes against what the best practices have to say.

Please advice on whether this is a reasonable approach or not? And, if not, why and what could be other superior alternatives?

Dec 18, 2018 in Others by Bharani
• 4,660 points

1 answer to this question.

0 votes
You wouldn't even need a finalizer if you released unmanaged resources only on application exit as then, the process unload would deal with them for you automatically.

Now, dealing with an app domain unload while having multiple app domains could become an issue but possibly one that you don't need to worry about.

I also agree that this type of design is probably not the right thing to do(will make it harder to fix, and subsequently you'll find yourself needing two instances). You could create the object (or just a lazy loading wrapper object) in your entry point and only pass it through the code to when and where it's needed, making it very clear as to who is responsible for providing it to whom. Then, you could always be free to change the decision of using only one instance with little effect to rest of the code(which only uses what it's given).
answered Dec 18, 2018 by nirvana
• 3,130 points

Related Questions In Others

0 votes
1 answer

How can I use wow.js and animate.css without affecting SEO?

there are two animation classes in MDBs ...READ MORE

answered Feb 22, 2022 in Others by narikkadan
• 63,700 points
0 votes
1 answer

"Server Tomcat v7.0 Server at localhost failed to start" without stack trace while it works in terminal

To resolve this issue, you have to ...READ MORE

answered Feb 23, 2022 in Others by Aditya
• 7,680 points
0 votes
0 answers
0 votes
1 answer

How use File("/sdcard/myfile") without READ_EXTERNAL_STORAGE on android 11?

You first need to use File.exists() and ...READ MORE

answered May 31, 2022 in Others by nisha
• 2,210 points
0 votes
1 answer
0 votes
2 answers

Is there a .NET equivalent to Apache Hadoop?

Hadoop is a Java-based platform. So, to ...READ MORE

answered Jul 16, 2020 in Big Data Hadoop by Suhana
• 340 points
0 votes
1 answer

Validate String against USPS State Abbreviations

Try something like this: private static String states ...READ MORE

answered Sep 20, 2018 in IoT (Internet of Things) by Annie97
• 2,160 points
0 votes
1 answer

Authenticate on an ASP.Net Forms Authorization website from a console app

Essentially, we need to record a regular ...READ MORE

answered Sep 20, 2018 in IoT (Internet of Things) by Annie97
• 2,160 points
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP