Singleton object doesn't detroy when i assign it to null.
-
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
-
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
-
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
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!
**
-
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
-
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
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
-
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!
**
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
-
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
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
-
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
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
-
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
-
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
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? -
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.
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