i have a Singleton object that holding a proxy to WCF methods. This Singleton object called from several points in code include as COM object.
My Question is:
- Is The
GCcan decide to free the object even if i will use it later (for example in COM) ? - How can i Decide When
Dispose()This object? The use of~Finalizer()method is good idea ? or may be theGCwill decide to finalize it before i finished use it ? - Is the call
GC.KeepAlive(this)can resolve the problem ?
Thanks!
EDIT:
public class Singleton
{
private static Singleton instance = null;
public static Singleton GetInstance()
{
if (instance == null)
{
lock (syncObject)
{
if (instance == null)
{
instance = new Singleton();
}
}
}
return instance;
}
public void CallWcfMethod()
{
// ....
}
}
public class Class1
{
Singleton instance = Singleton.GetInstance();
public void CallWcfMethod()
{
instance.CallWcfMethod();
}
}
[ComVisible(true)]
public class Class2
{
Singleton instance = Singleton.GetInstance();
public void CallWcfMethod()
{
instance.CallWcfMethod();
}
}
If your instance is declared as static, it will not be collected as long as the AppDomain is still alive at which point you seldom need to do any specific clean up.