CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Sep 2002
    Posts
    3

    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#?

  2. #2
    Join Date
    Sep 2002
    Location
    Ohio, USA
    Posts
    5
    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

  3. #3
    Join Date
    May 2002
    Location
    Quebec City, Canada
    Posts
    374
    but then it's not a singleton
    Martin Breton
    3D vision software developer and system integrator.

  4. #4
    Join Date
    Oct 2001
    Location
    Norway
    Posts
    265
    Can't see anything wrong with your code. How are you determining that the finalizer is called?

  5. #5
    Join Date
    May 2002
    Location
    Florida USA
    Posts
    27
    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.

  6. #6
    Join Date
    Sep 2002
    Posts
    3
    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;
    }

  7. #7
    Join Date
    Oct 2001
    Location
    Norway
    Posts
    265
    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) )
         {
             //...
         }
    }

  8. #8
    Join Date
    Sep 2002
    Posts
    3
    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.

  9. #9
    Join Date
    Mar 2002
    Location
    Alpharetta, GA
    Posts
    51
    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
  •  





Click Here to Expand Forum to Full Width

Featured