It seems to me that OSG often uses singletons as a user convenience.
For example, because the osgDB registry is a singleton, one can simply write:

        osgDB::readNodeFile(file_path);

Instead of having to have a handle to a registry object:

        osgDB::Registry plugins;

        /* ... */

        osgDB::readNodeFile(file_path, plugins);


Which is great most of the time, 'cause you don't want to pass a 
osgDB::Registry references all over the place.

However, such a convenience comes at a high cost that is not necessarily 
obvious at first (see my previous post).

Implementing a singleton implies that there can only be one instance (although 
the "per-what" part is ambiguous). This is usually not motivated by a technical 
limitation, but by the developer who fails to see the need for more than one 
instance. Such assumptions are not necessarily wrong, but they do often put 
artificial limitations on how a class can be used (e.g. why can't I maintain 
two osgDB::Registry instances that have different plugins registered?).


In the case of OSG, my main complains with its use of singleton are:

- We cannot control the lifetime of objects created by OSG. OSG makes that 
decision for us, but we don't want OSG to tell our application what it should 
do. I expect OSG to do what our application tells it to do.

- Because of this, OSG singletons are broken when using multiple DLL/EXE and 
static libraries in an application. Our team had to call several obscure OSG 
release functions to work around the problem.

- The state of the application is changed magically and subtly by user function 
calls that seems unrelated. Because OSG is managing hidden global objects that 
are not exposed to the user, it makes it very difficult to understand the 
(global) states that are changed by some function.


I'm not saying there is no reason to use singletons in OSG. Quite the contrary. 
But I think that before using a singleton, it should be carefully considered 
whether it's due to a reasonable technical limitation or whether it's a mere 
convenience. What I don't like is that fact that currently OSG is deciding for 
me that my application should always use singleton implemented by OSG just for 
convenience, imposing all the limitations that go with it.

IMHO It should be possible to provide convenience and flexibility. For example, 
OSG 3.0 could be designed so that it minimizes the use of convenience 
singleton, and instead provide an extra "convenience" layer/library for 
applications that are happy with the default singleton behaviour.


Tanguy


 
-----Original Message-----
From: osg-users-boun...@lists.openscenegraph.org 
[mailto:osg-users-boun...@lists.openscenegraph.org] On Behalf Of Sukender
Sent: Thursday 12 February 2009 13:35
To: gor...@gordon-tomlinson.com; OpenSceneGraph Users
Subject: Re: [osg-users] memory leak false positives on Windows

Hi Tanguy, Robert and Gordon,

IMHO, there is no unique "singleton" pattern, but as much as existing 
implementations. The fact is that there is no perfect singleton, all have 
benefits and drawbacks. Several attemps were made to create a generic singleton 
(See boost.singleton candidate lib), but I never saw one that fits any use, and 
even in boost.singleton lib you don't have one, but multiple singletons.
I agree we may not throw away singletons (in general, not specifically talking 
about OSG), but we have to carefully understand, what each singleton does and 
does not, and then document and test it.

Thus, I suggest we create systematic unit tests (when 2.8.0 would be behind us) 
to ensure our singleton(s) do what we expect from them on *ALL* platforms 
supported by OSG. Maybe this could be the same for other low-level and/or 
cirtical code.
Your thoughts?

Sukender
PVLE - Lightweight cross-platform game engine - http://pvle.sourceforge.net/


Le Thu, 12 Feb 2009 14:10:42 +0100, Gordon Tomlinson 
<gor...@gordon-tomlinson.com> a écrit:

> Interesting claim on singletons  ;)
>
> I have to agree with Robert, singletons are an extremely useful tool but one
> does have to understand they have limitations due to in general their static
> nature and I also have disagree that pattern is broken, like anything they
> can be missed used  or over used, but I don't really see that in the OSG
> core
>
> The biggest issue I find is that so many programmers have no real
> understanding of  what the key word 'static' means to the life time of any
> variable, whether a POD or a singleton and that's the real problem ( and I
> know a lot of guys that are actually very good developers/programmers that
> do not understand the lifetime), now you compound this issue on today's PC
> that now finally support real multi- processors  and threaded apps sadly
> many yet simply do not have the back ground, experience, training again to
> understand the life of a static in those arenas or developed apps that are
> fully multi-processors  and thread
>
> This is good for old fossil idiots like myself and many on this list who cut
> our vis-sim teeth on good old SGI and it wonderful multi-processors threaded
> hardware/software environments, nothing like working with 32 processors, 14
> graphics window  outputs, 8 external various sim-engines feeding live in,
> add in quite a few in DIS/HLA connections now  those were the fun days ;)
>
> Just in case I don’t mean to say y'all don’t understand  , just that there a
> lot that don't more than you would think and I find that scary ...
>
> ____________________________________________________________________________
> __
> Gordon Tomlinson
>
> gor...@gordontomlinson.com
> IM: gordon3db...@3dscenegraph.com
> www.vis-sim.com www.gordontomlinson.com
> ____________________________________________________________________________
> __
>
> -----Original Message-----
> From: osg-users-boun...@lists.openscenegraph.org
> [mailto:osg-users-boun...@lists.openscenegraph.org] On Behalf Of Robert
> Osfield
> Sent: Thursday, February 12, 2009 7:32 AM
> To: OpenSceneGraph Users
> Subject: Re: [osg-users] memory leak false positives on Windows
>
> Hi Tanguy,
>
> I'm afraid I disagree with you on your assessment of the value of
> Singletons.  Yes you have to implement them correctly, but that goes
> with any solution for managing resources, throwing out use of
> Singletons doesn't not solve problems, it just introduces new ones.
>
> Robert.
>
> On Thu, Feb 12, 2009 at 12:00 PM, Tanguy Fautre
> <tang...@aristechnologies.com> wrote:
>> Hi,
>>
>> I know this is a bit OT, but talking about OSG design... What about
> dropping the singleton pattern entirely?
>> The more I use it or the more I encounter it, the more I think the
> singleton pattern is completely broken. I know this is quite a bold claim,
> but bare with me and let me explain.
>>
>>
>> - The life time of the singleton is not well defined in most cases,
> leading to typical problems in C++ such as the initialization order problem.
>>
>> - The singleton pattern is an ambiguous design. It states that there is
> one instance, but it does not state "per what". Is it per thread? Is it per
> process? Is it per library/DLL? Is it per application? Is it per OS? Is it
> per computer?
>>
>>
>> These two reasons alone make the typical singleton pattern broken in a lot
> of cases. Most of the time without the programmer realizing it (myself
> included). Anyone having to deal with singletons, static libraries and DLLs
> knows how dangerous it becomes (e.g. you can often have several unwanted
> instance of the singleton: one per DLL/EXE). Add thread-safety into the mix,
> and it all becomes a nightmare.
>>
>> We've had already several problems with OSG in our project due to that,
> and had to resort to ugly work arounds (e.g. manually calling undocumented
> OSG release functions).
>>
>>
>> May I suggest that a better design is investigated for OSG 3.0?
>>
>> For example:
>>
>> class Initializer : private noncopyable
>> {
>> public:
>>
>>        Initializer()
>>        {
>>                if (m_ref++ == 0)
>>                        initializeGlobalInstances();
>>        }
>>
>>        ~Initializer()
>>        {
>>                if (--m_ref == 0)
>>                        uninitializeGlobalInstances();
>>        }
>>
>> private:
>>
>>        initializeGlobalInstances();
>>        uninitializeGlobalInstances();
>>
>>        atomic_counter m_ref;
>> };
>>
>>
>> Such as solution clearly defines the lifetime of the OSG library. It also
> puts the responsibility of managing the lifetime of the library in the user
> hands, who's more apt to know what is right for his application. It's also
> exception-safe.
>>
>> Note that that implementing
> initializeGlobalInstances/uninitializeGlobalInstances as thread-safe needs
> careful thinking (see
> http://www.justsoftwaresolutions.co.uk/articles/implementing_mutexes.html
> especially the part about initialization and uninitialization).
>>
>> Also note that the Initializer has to be implemented in a DLL (i.e. shared
> library) and not a static library, as it does not solve the multiple
> singleton instances per process problem. However, I'm currently not aware of
> any robust ways of solving that problem.
>>
>>
>> I hope I don't come as too arrogant on this. But I really think this part
> of OSG doesn't do justice to the rest of the library.
>>
>> Cheers,
>>
>> Tanguy
>>
>>
>> -----Original Message-----
>> From: osg-users-boun...@lists.openscenegraph.org
> [mailto:osg-users-boun...@lists.openscenegraph.org] On Behalf Of
> Jean-Sébastien Guay
>> Sent: Wednesday 11 February 2009 16:21
>> To: OpenSceneGraph Users
>> Subject: Spam: Re: [osg-users] memory leak false positives on Windows
>>
>> Hi Cory,
>>
>>> I don't think anybody has questioned the design of OSG.
>>
>> Yes, Neil has said that perhaps we should consider "restructuring" to
>> avoid the false positives. This comes down to changing the design.
>>
>>> I also agree with you that making code concessions to accommodate tools
>>> is unfortunate, but it happens all the time.
>>
>> Yes, the coding changes to accomodate tools. But the design will stay
>> the same. It's the implementation that changes. That's what I was
>> talking about.
>>
>>> On the other hand, I'd like to know if code that explicitly unloads
>>> OSG would ever be accepted into the repository. I'm getting the sense
>>> that it would be if it were sufficiently transparent, simple and
>>> inexpensive.
>>
>> Yes, Robert said in this thread that he would accept something that
>> unloads/unrefs all singletons / static objects, but it might be hard to
>> get to all singletons since some exist in the implementation files only.
>>
>>>> (btw, has anyone compiled valgrind for Windows?)
>>> valgrind is Linux only.
>>
>> I suspect it's not Linux-specific, but more gcc/g++ specific, and so
>> might be buildable on cygwin or mingw? If it is, then it might be usable
>> for Windows executables...
>>
>> J-S
>> --
>> ______________________________________________________
>> Jean-Sebastien Guay    jean-sebastien.g...@cm-labs.com
>>                                http://www.cm-labs.com/
>>                         http://whitestar02.webhop.org/
>> _______________________________________________
>> osg-users mailing list
>> osg-users@lists.openscenegraph.org
>> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
>> _______________________________________________
>> osg-users mailing list
>> osg-users@lists.openscenegraph.org
>> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
>>
> _______________________________________________
> osg-users mailing list
> osg-users@lists.openscenegraph.org
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
>
> _______________________________________________
> osg-users mailing list
> osg-users@lists.openscenegraph.org
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

_______________________________________________
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
_______________________________________________
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to