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,550 points
70 views

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,060 points

Related Questions In Others

0 votes
1 answer
0 votes
1 answer

Is there a .NET equivalent to Apache Hadoop?

Microsoft dropped its alternative (Dryad) in favor of Hadoop. ...READ MORE

answered Sep 18, 2018 in Big Data Hadoop by Frankie
• 9,810 points
37 views
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,190 points
20 views
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,190 points
30 views
0 votes
1 answer

Can we Access AWS SNS service without having access to Secret key and access key?

Yes what you ask for is possible. ...READ MORE

answered May 9, 2018 in Cloud Computing by DragonLord999
• 8,380 points
45 views
0 votes
1 answer

How can I remove/hide/disable excessive HTTP response headers in Azure/IIS7 without having to use UrlScan?

MSDN published an article on how to ...READ MORE

answered May 21, 2018 in Azure by club_seesharp
• 3,450 points
367 views
0 votes
1 answer

How do I start jenkins as an admin without having the need of entering credentials?

I found a way around: Go to /Users/Username/.jenkins/ Open config.xml and remove ...READ MORE

answered Aug 22, 2018 in Jenkins by DareDev
• 6,810 points
47 views
0 votes
5 answers

Can I quit my job without having a new one?

No, that's a big problem for IT ...READ MORE

answered Apr 2 in Career Counselling by Sindhu
62 views
0 votes
3 answers

Can we run Spark without using Hadoop?

No, you can run spark without hadoop. ...READ MORE

answered May 7 in Big Data Hadoop by pradeep
121 views