Singleton implementation

Simon Hausmann hausmann at
Wed Jul 9 18:06:19 BST 2003

On Wed, Jul 09, 2003 at 11:48:33AM -0400, Zack Rusin wrote:
> On Wednesday 09 July 2003 04:42, Simon Hausmann wrote:
> > As much as I'd like to see a good re-usable template for singletons
> > (I was excited, too, about putting this into kdelibs, when I read
> > the book :) as much I'm afraid it's of very limited use for us. The
> > way the destruction is designed with atexit prevents the use of the
> > template in a library. Apart from very interesting points made by
> > Havoc on the use of atexit in libraries --
> >
> >00083.html -- there is another problem: Noone calls the function you
> > registered with atexit when dlclosing the library the function is in.
> This one wouldn't be a problem, since that's why I added 
> KDEDefaultLifetime which can function without atexit. I added it there 
> to perform the cleanup on the spot but could just leave to KGlobal to 
> clean it up. We could comment out other policies and use only that one.

Ah, right, I missed that it calls the function in libkdecore. Hmmmm,
but deleteStaticDeleters calls back
SingletonHolder<...>::destroySingleton() through KSD which doesn't
exist anymore if the lib got unloaded before. Hmm, although that
could probably be solved with another registration mechanism. Would
leave the problem of the leaked KSDs, which could be solved with
static objects, which brings us back to the second problem. Argh :}

Another idea was proposed on #kde-devel some time ago (by I think
Wimpie, but my memory could serve me wrong) : Using static objects
for the destruction and and bind the singleton template to the class
implementation through inheritance, like:

class YourClass : public Singleton< YourClass >

The disadvantage is that this API might easily scare off people :)
(plus there was some other problem, which I forgot though)


More information about the kde-core-devel mailing list