473,395 Members | 1,639 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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...infinitely...)
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.

Nov 15 '05 #1
8 1442
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)paldino=at=exisconsulting<dot>com

"Cybertof" <cy****************@gmx.net> wrote in message
news:MP************************@msnews.microsoft.c om...
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...infinitely...)
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.

Nov 15 '05 #2
Cybertof <cy****************@gmx.net> wrote:
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;

}
}


You should be aware that that's not thread safe. See
http://www.pobox.com/~skeet/csharp/singleton.html

See Nick's response to your actual question :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #3
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" <cy****************@gmx.net> wrote in message
news:MP************************@msnews.microsoft.c om...
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...infinitely...)
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.

Nov 15 '05 #4
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 <OJ**************@TK2MSFTNGP09.phx.gbl>, "Ignacio Machin
\( .NET/ C# MVP \)" <ignacio.machin AT dot.state.fl.us> says...
Hi,

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

Cheers,

Nov 15 '05 #5
Cybertof <cy****************@gmx.net> wrote:
What about if i let the Constructor public but modifying it like below ?


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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #6
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 <MP************************@msnews.microsoft.com >,
sk***@pobox.com says...

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.

Nov 15 '05 #7
Cybertof <cy****************@gmx.net> wrote:
What about a new 'public' constructor like this :

public CLockeEnv()
{
if(mLockeEnv == null)
mlockeEnv = this; // no more embedded instanciation
}
That would certainly avoid the recursion.
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.


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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #8
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" <cy****************@gmx.net> wrote in message
news:MP************************@msnews.microsoft.c om...
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 <MP************************@msnews.microsoft.com >,
sk***@pobox.com says...

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.

Nov 15 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: E. Robert Tisdale | last post by:
Could somebody please help me with the definition of a singleton? > cat singleton.cc class { private: // representation int A; int B; public: //functions
3
by: Alicia Roberts | last post by:
Hello everyone, I have been researching the Singleton Pattern. Since the singleton pattern uses a private constructor which in turn reduces extendability, if you make the Singleton Polymorphic...
3
by: Harry | last post by:
Hi ppl I have a doubt on singleton class. I am writing a program below class singleton { private: singleton(){}; public: //way 1
5
by: Pelle Beckman | last post by:
Hi, I've done some progress in writing a rather simple singleton template. However, I need a smart way to pass constructor arguments via the template. I've been suggested reading "Modern C++...
8
by: Robert Zurer | last post by:
I have a server application that makes a MarshalByReferenceObject available via remoting. It's lifetime is set to never expire and it is implemented as a Singleton. Are calls to this object...
7
by: Robert W. | last post by:
I've just been reading all about the Singleton class and understand how to implement and use it but I cannot understand why one NEEDS to use it instead of just declaring a class and implementing...
7
by: Adrian | last post by:
Hi, I have a large unmanaged static C++ library which I've wrapped using a small C++/CLR DLL. This is called from a C# client application. The static library has a singleton, however it appears...
2
by: EvilOldGit | last post by:
In the stuff I've read about multi threaded singleton it seems to assume that creating the Lock is atomic, and various sources, including Sutter go for RAII. { Lock l; if ( ! inst ).... } But...
3
weaknessforcats
by: weaknessforcats | last post by:
Design Pattern: The Singleton Overview Use the Singleton Design Pattern when you want to have only one instance of a class. This single instance must have a single global point of access. That...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.