|
-
September 17th, 2002, 05:05 PM
#1
Instance of singleton class is getting garbage collected
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/de...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#?
-
September 22nd, 2002, 06:38 PM
#2
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
-
September 22nd, 2002, 09:27 PM
#3
but then it's not a singleton
Martin Breton
3D vision software developer and system integrator.
-
September 23rd, 2002, 05:24 AM
#4
Can't see anything wrong with your code. How are you determining that the finalizer is called?
-
September 23rd, 2002, 05:30 AM
#5
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
Last edited by bfarley; September 23rd, 2002 at 05:35 AM.
-
September 23rd, 2002, 12:49 PM
#6
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;
}
-
September 25th, 2002, 09:14 AM
#7
From what I know about .NET GC, this shouldnt be possible 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:
Code:
public class Bleh
{
public static void BlehMethod()
{
lock( typeof(Bleh) )
{
//...
}
}
-
September 25th, 2002, 11:39 AM
#8
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.
-
September 25th, 2002, 04:12 PM
#9
Have you tried adding the object to the Application collection?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|