-1

I want to release the unmanaged resource from the static class.

What i tried: I have made the class as singleton instead of static. and added a destructor. but the unmanaged resource is not geting release immediately. since destructor will not get called immediately like dispose.

and I am not implementing Idisposable since the i cannot call dispose method from the user class.

Taras Shcherban
  • 187
  • 2
  • 11
Ysr
  • 31
  • 5
  • 4
    Why the static class needs unmanaged resources? What kind of objects? – Tim Schmelter Nov 17 '17 at 09:56
  • static class is not being disposed, so you should dispose your resources explicitly either via `using` or manually calling `Dispose` – Taras Shcherban Nov 17 '17 at 09:57
  • 1
    I'm not sure having an unmanaged resource that needs to be disposed in either a singleton or a static class is a good design... – Matt Hogan-Jones Nov 17 '17 at 09:57
  • 1
    Refer this : https://stackoverflow.com/questions/12126598/how-and-when-are-c-sharp-static-members-disposed – Binu Vijayan Nov 17 '17 at 09:58
  • 1
    _"I have made the class as singleton instead of static. and added a destructor. but the unmanaged resource is not geting release immediately. since destructor will not get called immediately like dispose"_ I don't get it. Why you expect that it gets disposed immediately? Every static member(like the singleton) is disposed when the app domain is unloaded, not when you are finished with it. I'm pretty sure your static class should create these unmanaged resources in their static methods and take care that they get disposed immediately(f.e. by using the `using`-statement). – Tim Schmelter Nov 17 '17 at 10:05
  • 1
    I'm with Tim - If you really, really, really, really need a static method that uses IDisposable instances or unmanaged resources, use them locally inside your methods, with the using statement. Any other way is a potential memory leak. – Zohar Peled Nov 17 '17 at 10:12
  • 4
    This is an X/Y question. Your main approach is wrong and now you want us to fix a small detail. – H H Nov 17 '17 at 10:23

1 Answers1

1

Assuming you have a good reason to hold onto an object for the lifetime of the application (see comments section), you still shouldn't store it in a static variable. Instead, store it in an object that represents the lifepsan of the application; for example, in a Windows Forms application, you could store it as a member variable of the main form, and in a web application you could store it as a member of your HttpApplication (global.asax.cs). Both a form and a web application are themselves disposable, so you can override and extend their Dispose method and dispose of your unmanaged resource there. Both Disposes will be called implicitly when the application exits.

class MainForm : Form
{
    private readonly AutoResetEvent _global = new AutoResetEvent();

    public override void Dispose()
    {
        _global.Dispose();
        base.Dispose();
    }
}
John Wu
  • 50,556
  • 8
  • 44
  • 80