Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Singleton object doesn't detroy when i assign it to null.

Singleton object doesn't detroy when i assign it to null.

Scheduled Pinned Locked Moved C#
question
11 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    madhusri
    wrote on last edited by
    #1

    Here is the code sample of sington class and their clients public class single { public string str = "Hello"; private static single s = null; private single() { } public static single CreateInstance() { if (s == null) s = new single(); return s; } public string Get() { return str; } } class Program { static void Main(string[] args) { single i = single.CreateInstance(); string str1 = i.Get(); Console.WriteLine(str1); i.str = "World"; i = null; single j = single.CreateInstance(); string str2 = j.Get(); Console.WriteLine(str2); j = single.CreateInstance(); j.str = "New"; string str3 = j.Get(); Console.WriteLine(str3); } } Output:: Hello World New But it should be Hello Hello New since we assigned i=null then the singleton must be destroyed and new instance should be created when j = single.CreateInstance(); executed right?

    Thanks and Regards Madhu

    R L V C 4 Replies Last reply
    0
    • M madhusri

      Here is the code sample of sington class and their clients public class single { public string str = "Hello"; private static single s = null; private single() { } public static single CreateInstance() { if (s == null) s = new single(); return s; } public string Get() { return str; } } class Program { static void Main(string[] args) { single i = single.CreateInstance(); string str1 = i.Get(); Console.WriteLine(str1); i.str = "World"; i = null; single j = single.CreateInstance(); string str2 = j.Get(); Console.WriteLine(str2); j = single.CreateInstance(); j.str = "New"; string str3 = j.Get(); Console.WriteLine(str3); } } Output:: Hello World New But it should be Hello Hello New since we assigned i=null then the singleton must be destroyed and new instance should be created when j = single.CreateInstance(); executed right?

      Thanks and Regards Madhu

      R Offline
      R Offline
      Ravi Bhavnani
      wrote on last edited by
      #2

      No, since i is a local variable. You're just nulling your local reference of the singleton (which continues to exist). /ravi

      My new year's resolution: 2048 x 1536 Home | Music | Articles | Freeware | Trips ravib(at)ravib(dot)com

      1 Reply Last reply
      0
      • M madhusri

        Here is the code sample of sington class and their clients public class single { public string str = "Hello"; private static single s = null; private single() { } public static single CreateInstance() { if (s == null) s = new single(); return s; } public string Get() { return str; } } class Program { static void Main(string[] args) { single i = single.CreateInstance(); string str1 = i.Get(); Console.WriteLine(str1); i.str = "World"; i = null; single j = single.CreateInstance(); string str2 = j.Get(); Console.WriteLine(str2); j = single.CreateInstance(); j.str = "New"; string str3 = j.Get(); Console.WriteLine(str3); } } Output:: Hello World New But it should be Hello Hello New since we assigned i=null then the singleton must be destroyed and new instance should be created when j = single.CreateInstance(); executed right?

        Thanks and Regards Madhu

        L Offline
        L Offline
        leppie
        wrote on last edited by
        #3

        madhusri wrote:

        since we assigned i=null then the singleton must be destroyed and new instance should be created when j = single.CreateInstance(); executed right?

        No, that defeats the goal of the singleton pattern.

        **

        xacc.ide-0.2.0.50 - now with partial MSBuild support!

        **

        M 1 Reply Last reply
        0
        • M madhusri

          Here is the code sample of sington class and their clients public class single { public string str = "Hello"; private static single s = null; private single() { } public static single CreateInstance() { if (s == null) s = new single(); return s; } public string Get() { return str; } } class Program { static void Main(string[] args) { single i = single.CreateInstance(); string str1 = i.Get(); Console.WriteLine(str1); i.str = "World"; i = null; single j = single.CreateInstance(); string str2 = j.Get(); Console.WriteLine(str2); j = single.CreateInstance(); j.str = "New"; string str3 = j.Get(); Console.WriteLine(str3); } } Output:: Hello World New But it should be Hello Hello New since we assigned i=null then the singleton must be destroyed and new instance should be created when j = single.CreateInstance(); executed right?

          Thanks and Regards Madhu

          V Offline
          V Offline
          V 0
          wrote on last edited by
          #4

          I think you need to dispose the object out of memory. Assigning null is no different then assigning a value. The object itself still exists.

          I've found a living worth working for, but I haven't found work worth living for. :beer:
          :jig:

          1 Reply Last reply
          0
          • M madhusri

            Here is the code sample of sington class and their clients public class single { public string str = "Hello"; private static single s = null; private single() { } public static single CreateInstance() { if (s == null) s = new single(); return s; } public string Get() { return str; } } class Program { static void Main(string[] args) { single i = single.CreateInstance(); string str1 = i.Get(); Console.WriteLine(str1); i.str = "World"; i = null; single j = single.CreateInstance(); string str2 = j.Get(); Console.WriteLine(str2); j = single.CreateInstance(); j.str = "New"; string str3 = j.Get(); Console.WriteLine(str3); } } Output:: Hello World New But it should be Hello Hello New since we assigned i=null then the singleton must be destroyed and new instance should be created when j = single.CreateInstance(); executed right?

            Thanks and Regards Madhu

            C Offline
            C Offline
            Colin Angus Mackay
            wrote on last edited by
            #5

            madhusri wrote:

            since we assigned i=null then the singleton must be destroyed and new instance should be created when j = single.CreateInstance(); executed right?

            Nope, wrong! See this code from CreateInstance:

            if (s == null)
            s = new single();
            return s;

            What happens here is the one and only ever instance never to be repeated is created if it does not exist and stored in the static field called s. s is always returned. Nothing else ever assigns to s therefore the singleton pattern is maintained becuase you have ensured that there will only ever be one and only one ever instance of the object. This is the goal of the singleton, the behaviour you expect is at odds with the pattern you have chosen.


            Scottish Developers events: * .NET debugging, tracing and instrumentation by Duncan Edwards Jones and Code Coverage in .NET by Craig Murphy * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog

            1 Reply Last reply
            0
            • L leppie

              madhusri wrote:

              since we assigned i=null then the singleton must be destroyed and new instance should be created when j = single.CreateInstance(); executed right?

              No, that defeats the goal of the singleton pattern.

              **

              xacc.ide-0.2.0.50 - now with partial MSBuild support!

              **

              M Offline
              M Offline
              madhusri
              wrote on last edited by
              #6

              Hi leppie, When i = null is assigned, there are no reference pointers pointing to the singleton object. Hence if there is a GC, then the singleton object has to be deleted. In this case, next time an instance to the singleton object is created, it should be a new object again. But the old object is being retained and returned when i call createInstance() again. Basically, when will the singleton object be destroyed from memory? We understand that an object will be deleted if there are no references to that object or if the object is set to null. Is this right?

              Thanks and Regards Madhu

              E C 2 Replies Last reply
              0
              • M madhusri

                Hi leppie, When i = null is assigned, there are no reference pointers pointing to the singleton object. Hence if there is a GC, then the singleton object has to be deleted. In this case, next time an instance to the singleton object is created, it should be a new object again. But the old object is being retained and returned when i call createInstance() again. Basically, when will the singleton object be destroyed from memory? We understand that an object will be deleted if there are no references to that object or if the object is set to null. Is this right?

                Thanks and Regards Madhu

                E Offline
                E Offline
                Ennis Ray Lynch Jr
                wrote on last edited by
                #7

                When all object references are removed it is scheduled for garbage collection. The first round of the GC does the clean up. The next time the GC runs it removes it. If you need to free resources implement the IDisposable method and call that before assigning null and then just don't worry about it.

                A man said to the universe: "Sir I exist!" "However," replied the Universe, "The fact has not created in me A sense of obligation." -- Stephen Crane

                E 1 Reply Last reply
                0
                • M madhusri

                  Hi leppie, When i = null is assigned, there are no reference pointers pointing to the singleton object. Hence if there is a GC, then the singleton object has to be deleted. In this case, next time an instance to the singleton object is created, it should be a new object again. But the old object is being retained and returned when i call createInstance() again. Basically, when will the singleton object be destroyed from memory? We understand that an object will be deleted if there are no references to that object or if the object is set to null. Is this right?

                  Thanks and Regards Madhu

                  C Offline
                  C Offline
                  Colin Angus Mackay
                  wrote on last edited by
                  #8

                  madhusri wrote:

                  When i = null is assigned, there are no reference pointers pointing to the singleton object.

                  This is incorrect. The class you showed has a static reference to its one and only instance.

                  madhusri wrote:

                  But the old object is being retained and returned when i call createInstance() again.

                  And it manages this because something is holding a reference that the CreateInstance() method can pass back.

                  madhusri wrote:

                  We understand that an object will be deleted if there are no references to that object or if the object is set to null. Is this right?

                  The first part is correct. The second part is incorrect because you are not setting the object to null, not now, not ever. What you are doing is setting a reference to the object to null.


                  Scottish Developers events: * .NET debugging, tracing and instrumentation by Duncan Edwards Jones and Code Coverage in .NET by Craig Murphy * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog

                  E 1 Reply Last reply
                  0
                  • E Ennis Ray Lynch Jr

                    When all object references are removed it is scheduled for garbage collection. The first round of the GC does the clean up. The next time the GC runs it removes it. If you need to free resources implement the IDisposable method and call that before assigning null and then just don't worry about it.

                    A man said to the universe: "Sir I exist!" "However," replied the Universe, "The fact has not created in me A sense of obligation." -- Stephen Crane

                    E Offline
                    E Offline
                    engsrini
                    wrote on last edited by
                    #9

                    Event we tried to GC the i object after assigning to null ie. i=null; GC.Collect(); Thread.Sleep(2000);// Waitin for some time to Collect the object i by GC even this never destroyed the object.

                    E 1 Reply Last reply
                    0
                    • C Colin Angus Mackay

                      madhusri wrote:

                      When i = null is assigned, there are no reference pointers pointing to the singleton object.

                      This is incorrect. The class you showed has a static reference to its one and only instance.

                      madhusri wrote:

                      But the old object is being retained and returned when i call createInstance() again.

                      And it manages this because something is holding a reference that the CreateInstance() method can pass back.

                      madhusri wrote:

                      We understand that an object will be deleted if there are no references to that object or if the object is set to null. Is this right?

                      The first part is correct. The second part is incorrect because you are not setting the object to null, not now, not ever. What you are doing is setting a reference to the object to null.


                      Scottish Developers events: * .NET debugging, tracing and instrumentation by Duncan Edwards Jones and Code Coverage in .NET by Craig Murphy * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog

                      E Offline
                      E Offline
                      engsrini
                      wrote on last edited by
                      #10

                      Now we understand. Out situation is like One exe will invoke function in 2 separate dlls using reflection. first exe loaded the dlls. those dlls create log object using CreateInstance(). suppose if the dll1 assigned the logobject.Moudulename="First"; //Variable in log object logobject=null; GC.Collect(); then the exe unloaded this dll1 and try to access dll2 function which uses logobject instance, will the changes made by dll1 will be afected to dll2's Logobject? if yes means the logobjected is collected by GC right so it will be no longer available to dll2 rite?

                      1 Reply Last reply
                      0
                      • E engsrini

                        Event we tried to GC the i object after assigning to null ie. i=null; GC.Collect(); Thread.Sleep(2000);// Waitin for some time to Collect the object i by GC even this never destroyed the object.

                        E Offline
                        E Offline
                        Ennis Ray Lynch Jr
                        wrote on last edited by
                        #11

                        And if custom memory management is so important you shouldn't be using C#.

                        A man said to the universe: "Sir I exist!" "However," replied the Universe, "The fact has not created in me A sense of obligation." -- Stephen Crane

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups