Singleton + IDisposable

T

Tom

Hi there.

I wonder how a singleton could be (auto)disposed.

For instance :

class Foo : IDisposable
{
private static readonly _instance = new Foo();
private Foo()
{
// opens a file or does anything that needs cleanup
}
public void Dispose()
{
}
public static Foo Instance
{
get { return( _instance ); }
}
}
 
R

Richard Blewett [DevelopMentor]

In what situation will you want to dispose it if it is a singleton? It means the singleton will become unusable for the rest of the lifetime of the application (well AppDomain to be more precise)

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Hi there.

I wonder how a singleton could be (auto)disposed.

For instance :

class Foo : IDisposable
{
private static readonly _instance = new Foo();
private Foo()
{
// opens a file or does anything that needs cleanup
}
public void Dispose()
{
}
public static Foo Instance
{
get { return( _instance ); }
}
}
 
W

Wiebe Tijsma

Tom said:
Well I only need to dispose it at the end of the application lifetime.

If I'm correct, the singleton is disposed anyway when the application
ends, because the AppDomain is disposed, also disposing all instantiated
variables within that AppDomain...

Maybe you can try to set a breakpoint there, not sure if the debugger
will still get there when an application is exiting.

Wiebe
 
R

Richard Blewett [DevelopMentor]

IDisposable is about cleaning up resources in a timely fashion without having to wait for a GC and finalization. As your application is going down anyway you might as well just let any finalizers run rather than run the risk of someone disposing the singleton and making it unusable for everyone else.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Well I only need to dispose it at the end of the application lifetime.
 
R

Richard Blewett [DevelopMentor]

Nope, the runtime *never* Disposes anything at all. Dispose is called by code not by the runtime. You're proably thinking about finalizers

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk
Well I only need to dispose it at the end of the application lifetime.

If I'm correct, the singleton is disposed anyway when the application
ends, because the AppDomain is disposed, also disposing all instantiated
variables within that AppDomain...

Maybe you can try to set a breakpoint there, not sure if the debugger
will still get there when an application is exiting.

Wiebe
 
W

Wiebe Tijsma

Richard said:
Nope, the runtime *never* Disposes anything at all. Dispose is called by code not by the runtime. You're proably thinking about finalizers

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk


If I'm correct, the singleton is disposed anyway when the application
ends, because the AppDomain is disposed, also disposing all instantiated
variables within that AppDomain...

Maybe you can try to set a breakpoint there, not sure if the debugger
will still get there when an application is exiting.

Wiebe

Hmm you're right, it doesn't excplicitly get called. You can assume
though it works this way for all standard .NET framework classes..

From MSDN:
"Because the Dispose method must be called explicitly, objects that
implement IDisposable must also implement a finalizer to handle freeing
resources when Dispose is not called. "
http://msdn.microsoft.com/library/d...l/frlrfSystemIDisposableClassDisposeTopic.asp

Wiebe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top