Singleton Examination (Watch)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Cybertof

    Singleton Examination (Watch)

    Hello,


    I need some help about a strange thing.
    I have the following declaration :


    public class CLockeEnv
    {
    private static CLockeEnv mlockeEnv;

    public CLockeEnv()
    {
    }

    public static CLockeEnv GetInstance()
    {
    if(mlockeEnv == null)
    mlockeEnv = new CLockeEnv();
    // Returns existing instance in all cases
    return mlockeEnv;

    }
    }


    How do you explain than in the 'Locals/Watch' window, i have an infinite
    hierarchical tree like :

    mLockeEnv
    - System.Obkect
    - mLockeEnv
    - System.Object
    - mLockeEnv
    - SystemObject
    - mLockeEnv
    - System.Object
    + mLockeEnv
    (etc...infinite ly...)


    What are all these 'mLockeEnv' ?....do they consume memory ?....i don't
    think so as they are not referenced (apart of the first one....), but
    why does not the view stop to the first inner level ?


    Regards,
    Christophe.

  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Singleton Examination (Watch)

    Cybertof,

    They shouldn't. Each instance also shows the static members associated
    with the type. Because there is an instance held by the static reference
    that you store, when debugging, you can keep going down further and further.

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - nick(dot)paldin o=at=exisconsul ting<dot>com

    "Cybertof" <cybertof2003no spam@gmx.net> wrote in message
    news:MPG.19ecdb 4fd7b9925798968 8@msnews.micros oft.com...[color=blue]
    > Hello,
    >
    >
    > I need some help about a strange thing.
    > I have the following declaration :
    >
    >
    > public class CLockeEnv
    > {
    > private static CLockeEnv mlockeEnv;
    >
    > public CLockeEnv()
    > {
    > }
    >
    > public static CLockeEnv GetInstance()
    > {
    > if(mlockeEnv == null)
    > mlockeEnv = new CLockeEnv();
    > // Returns existing instance in all cases
    > return mlockeEnv;
    >
    > }
    > }
    >
    >
    > How do you explain than in the 'Locals/Watch' window, i have an infinite
    > hierarchical tree like :
    >
    > mLockeEnv
    > - System.Obkect
    > - mLockeEnv
    > - System.Object
    > - mLockeEnv
    > - SystemObject
    > - mLockeEnv
    > - System.Object
    > + mLockeEnv
    > (etc...infinite ly...)
    >
    >
    > What are all these 'mLockeEnv' ?....do they consume memory ?....i don't
    > think so as they are not referenced (apart of the first one....), but
    > why does not the view stop to the first inner level ?
    >
    >
    > Regards,
    > Christophe.
    >[/color]


    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: Singleton Examination (Watch)

      Cybertof <cybertof2003no spam@gmx.net> wrote:[color=blue]
      > I need some help about a strange thing.
      > I have the following declaration :
      >
      >
      > public class CLockeEnv
      > {
      > private static CLockeEnv mlockeEnv;
      >
      > public CLockeEnv()
      > {
      > }
      >
      > public static CLockeEnv GetInstance()
      > {
      > if(mlockeEnv == null)
      > mlockeEnv = new CLockeEnv();
      > // Returns existing instance in all cases
      > return mlockeEnv;
      >
      > }
      > }[/color]

      You should be aware that that's not thread safe. See
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.


      See Nick's response to your actual question :)

      --
      Jon Skeet - <skeet@pobox.co m>
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

      If replying to the group, please do not mail me too

      Comment

      • Ignacio Machin \( .NET/ C#  MVP \)

        #4
        Re: Singleton Examination (Watch)

        Hi,

        One observation
        If you are tryig to implement the singleton pattern your constructor should
        be private, not public.

        Cheers,

        --
        Ignacio Machin,
        ignacio.machin AT dot.state.fl.us
        Florida Department Of Transportation

        "Cybertof" <cybertof2003no spam@gmx.net> wrote in message
        news:MPG.19ecdb 4fd7b9925798968 8@msnews.micros oft.com...[color=blue]
        > Hello,
        >
        >
        > I need some help about a strange thing.
        > I have the following declaration :
        >
        >
        > public class CLockeEnv
        > {
        > private static CLockeEnv mlockeEnv;
        >
        > public CLockeEnv()
        > {
        > }
        >
        > public static CLockeEnv GetInstance()
        > {
        > if(mlockeEnv == null)
        > mlockeEnv = new CLockeEnv();
        > // Returns existing instance in all cases
        > return mlockeEnv;
        >
        > }
        > }
        >
        >
        > How do you explain than in the 'Locals/Watch' window, i have an infinite
        > hierarchical tree like :
        >
        > mLockeEnv
        > - System.Obkect
        > - mLockeEnv
        > - System.Object
        > - mLockeEnv
        > - SystemObject
        > - mLockeEnv
        > - System.Object
        > + mLockeEnv
        > (etc...infinite ly...)
        >
        >
        > What are all these 'mLockeEnv' ?....do they consume memory ?....i don't
        > think so as they are not referenced (apart of the first one....), but
        > why does not the view stop to the first inner level ?
        >
        >
        > Regards,
        > Christophe.
        >[/color]


        Comment

        • Cybertof

          #5
          Re: Singleton Examination (Watch)

          What about if i let the Constructor public but modifying it like below ?

          (instanciating only if no previous instanciation was done, even from
          Constructor or from GetInstance method)

          public class CLockeEnv
          {
          private static CLockeEnv mlockeEnv;

          public CLockeEnv()
          {
          if(mLockeEnv == null)
          mlockeEnv = new CLockeEnv();
          }

          public static CLockeEnv GetInstance()
          {
          if(mlockeEnv == null)
          mlockeEnv = new CLockeEnv();
          // Returns existing instance in all cases
          return mlockeEnv;

          }
          }


          In article <OJ8dwsNjDHA.20 76@TK2MSFTNGP09 .phx.gbl>, "Ignacio Machin
          \( .NET/ C# MVP \)" <ignacio.mach in AT dot.state.fl.us > says...[color=blue]
          > Hi,
          >
          > One observation
          > If you are tryig to implement the singleton pattern your constructor should
          > be private, not public.
          >
          > Cheers,
          >
          >[/color]

          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: Singleton Examination (Watch)

            Cybertof <cybertof2003no spam@gmx.net> wrote:[color=blue]
            > What about if i let the Constructor public but modifying it like below ?[/color]

            As it is, you'll end up with a stack overflow the first time you call
            the constructor (mlockEnv doesn't get assigned until the "embedded"
            constructor call finishes, so the embedded constructor call sees that
            mlockEnv is null, and calls the constructor again, etc). Basically, if
            you have a non-private constructor you *can't* guarantee that there'll
            only be one instance of your class, which is the purpose of the
            singleton.

            --
            Jon Skeet - <skeet@pobox.co m>
            Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

            If replying to the group, please do not mail me too

            Comment

            • Cybertof

              #7
              Re: Singleton Examination (Watch)

              That's true.

              What about a new 'public' constructor like this :

              public CLockeEnv()
              {
              if(mLockeEnv == null)
              mlockeEnv = this; // no more embedded instanciation
              }


              Note :
              *******
              No more embedded object instanciation, no stack overflow.
              The instance is unique as if it already exists, it does not instanciate
              anymore in the constructor, but the first time it was, it flags
              mLockeEnv saying 'i'm existing'.
              So, apart of thread safety aspect, the singleton can see its constructor
              called many times, but only the first time is the first creation of the
              object (using the new keyword), or passing through the GetInstance()
              method.


              (final code)

              public class CLockeEnv
              {
              private static CLockeEnv mlockeEnv;

              public CLockeEnv()
              {
              if(mLockeEnv == null)
              mlockeEnv = this;
              }

              public static CLockeEnv GetInstance()
              {
              if(mlockeEnv == null)
              mlockeEnv = new CLockeEnv();
              // Returns existing instance in all cases
              return mlockeEnv;

              }
              }



              In article <MPG.19ecdb0e77 f87e70989817@ms news.microsoft. com>,
              skeet@pobox.com says...[color=blue]
              >
              > As it is, you'll end up with a stack overflow the first time you call
              > the constructor (mlockEnv doesn't get assigned until the "embedded"
              > constructor call finishes, so the embedded constructor call sees that
              > mlockEnv is null, and calls the constructor again, etc). Basically, if
              > you have a non-private constructor you *can't* guarantee that there'll
              > only be one instance of your class, which is the purpose of the
              > singleton.
              >[/color]

              Comment

              • Jon Skeet [C# MVP]

                #8
                Re: Singleton Examination (Watch)

                Cybertof <cybertof2003no spam@gmx.net> wrote:[color=blue]
                > What about a new 'public' constructor like this :
                >
                > public CLockeEnv()
                > {
                > if(mLockeEnv == null)
                > mlockeEnv = this; // no more embedded instanciation
                > }[/color]

                That would certainly avoid the recursion.
                [color=blue]
                > Note :
                > *******
                > No more embedded object instanciation, no stack overflow.
                > The instance is unique as if it already exists, it does not instanciate
                > anymore in the constructor, but the first time it was, it flags
                > mLockeEnv saying 'i'm existing'.
                > So, apart of thread safety aspect, the singleton can see its constructor
                > called many times, but only the first time is the first creation of the
                > object (using the new keyword), or passing through the GetInstance()
                > method.[/color]

                No, an object is *always* created when the constructor is called - the
                fact that you're in the constructor gives that!

                Why are you desperate to avoid making the constructor private?

                --
                Jon Skeet - <skeet@pobox.co m>
                Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                If replying to the group, please do not mail me too

                Comment

                • Ignacio Machin \( .NET/ C#  MVP \)

                  #9
                  Re: Singleton Examination (Watch)

                  Hi,

                  Why no make the constructor private and all the problems gets away?

                  What you have agains it?

                  That's the only way to be sure that it will be created only once, that is
                  the whole point of the singleton !!!

                  Cheers,

                  --
                  Ignacio Machin,
                  ignacio.machin AT dot.state.fl.us
                  Florida Department Of Transportation

                  "Cybertof" <cybertof2003no spam@gmx.net> wrote in message
                  news:MPG.19ecec 528d0e948f98968 d@msnews.micros oft.com...[color=blue]
                  > That's true.
                  >
                  > What about a new 'public' constructor like this :
                  >
                  > public CLockeEnv()
                  > {
                  > if(mLockeEnv == null)
                  > mlockeEnv = this; // no more embedded instanciation
                  > }
                  >
                  >
                  > Note :
                  > *******
                  > No more embedded object instanciation, no stack overflow.
                  > The instance is unique as if it already exists, it does not instanciate
                  > anymore in the constructor, but the first time it was, it flags
                  > mLockeEnv saying 'i'm existing'.
                  > So, apart of thread safety aspect, the singleton can see its constructor
                  > called many times, but only the first time is the first creation of the
                  > object (using the new keyword), or passing through the GetInstance()
                  > method.
                  >
                  >
                  > (final code)
                  >
                  > public class CLockeEnv
                  > {
                  > private static CLockeEnv mlockeEnv;
                  >
                  > public CLockeEnv()
                  > {
                  > if(mLockeEnv == null)
                  > mlockeEnv = this;
                  > }
                  >
                  > public static CLockeEnv GetInstance()
                  > {
                  > if(mlockeEnv == null)
                  > mlockeEnv = new CLockeEnv();
                  > // Returns existing instance in all cases
                  > return mlockeEnv;
                  >
                  > }
                  > }
                  >
                  >
                  >
                  > In article <MPG.19ecdb0e77 f87e70989817@ms news.microsoft. com>,
                  > skeet@pobox.com says...[color=green]
                  > >
                  > > As it is, you'll end up with a stack overflow the first time you call
                  > > the constructor (mlockEnv doesn't get assigned until the "embedded"
                  > > constructor call finishes, so the embedded constructor call sees that
                  > > mlockEnv is null, and calls the constructor again, etc). Basically, if
                  > > you have a non-private constructor you *can't* guarantee that there'll
                  > > only be one instance of your class, which is the purpose of the
                  > > singleton.
                  > >[/color][/color]


                  Comment

                  Working...