Click to See Complete Forum and Search --> : Instance of singleton class is getting garbage collected
anshugoel
September 17th, 2002, 05:05 PM
I have a singleton class implemented as follows-
// .NET Singleton
sealed class Singleton
{
private Singleton() {}
public static readonly Singleton Instance = new Singleton();
}
as suggested on the MSDN website (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/singletondespatt.asp?frame=true)
However, the instance keeps getting garbage collected (that is, the destructor is called).
I read an article that said this was a bug in Java 1.1. Is it the same with C#?
ianhoc2
September 22nd, 2002, 06:38 PM
You are trying to create an instance of your class within the class itself.
Call Singleton clSingleton = new Singleton() from within your main code when you need the class to be created.
Ian
proxima centaur
September 22nd, 2002, 09:27 PM
but then it's not a singleton ;)
Arild Fines
September 23rd, 2002, 05:24 AM
Can't see anything wrong with your code. How are you determining that the finalizer is called?
bfarley
September 23rd, 2002, 05:30 AM
You need to have logic in your singleton class constructor or some "instance()" method to determine if the singleton has already been instantiated. If it has, you just pass a ref, if it has not, you create it then pass a ref. By only creating the instance when you are going to hand somebody a ref, you ensure it won't go out of scope and get GC'd.
Bill F
anshugoel
September 23rd, 2002, 12:49 PM
Arild, I put a log in the finalizer and I am starting the debugger and putting a break point here; that's how I know that it's being called. Here's the code -
~SessionManager()
{
Log.LogRequest("c:\\debug\\debug.xml", "session manager", "destructor", "hashcode"+this.GetHashCode());
System.Diagnostics.Debugger.Launch();
System.Diagnostics.Debugger.Break();
}
Bill, I also tried the following and it's GC'd too.
private static SessionManager m_smInstance = null;
private static object m_oSyncRoot = new Object();
public static SessionManager GetInstance()
{
if (m_smInstance == null)
{
lock (m_oSyncRoot)
{
if (m_smInstance == null)
{
m_smInstance = new SessionManager();
}
}
}
return m_smInstance;
}
Arild Fines
September 25th, 2002, 09:14 AM
From what I know about .NET GC, this shouldnt be possible :rolleyes: You absolutely sure you're not nulling the reference to it by accident somewhere? Does this happen if you force a collect manually?
PS - You don't need a dummy object to do a lock in a static method. This is the preferred way:
public class Bleh
{
public static void BlehMethod()
{
lock( typeof(Bleh) )
{
//...
}
}
anshugoel
September 25th, 2002, 11:39 AM
Yes, you are right. I wasn't nulling the reference, but my code was being executed by ASP.NET (aspnet_wp process). I guess there are no guarantees that one's instance would be around all the time in ASP.NET.
mikescham
September 25th, 2002, 04:12 PM
Have you tried adding the object to the Application collection?
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.