[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-core-devel
Subject:    Re: strange KStaticDeleter usage
From:       "Thiago Macieira" <thiago () kde ! org>
Date:       2007-04-26 12:43:17
Message-ID: 1996.172.26.0.1.1177591397.squirrel () 172 ! 26 ! 0 ! 1
[Download RAW message or body]


David Faure said:
> On Thursday 26 April 2007, Thiago Macieira wrote:
>> David Faure wrote:
>> >This is a hook for applications like kontact, which can reimplement the
>> > behavior. kontact creates and instanciates a derived class, which sets
>> > it as the singleton instead of the kdelibs one, and everything goes
>> > through it instead of using the default implementation from kdelibs.
>>
>> That's a pure singleton behaviour, which is acceptable. It's not a
>> global
>> static behaviour.
>
> And how do you implement a singleton (with a base and a derived class)
> without a global static? Apart from using KStaticDeleter? :-)
> We need something that deletes singletons at the end of the process, to
> make
> memory leak debugging simpler, and that's what KStaticDeleter was doing up
> to now.

Like this:

KMySingleton *singleton;
static void deleteSingleton()
{
    delete singleton;
    singleton = 0;
}

static void setSingleton(KMySingleton *ptr)
{
    delete singleton;
    singleton = ptr;
    qAddPostRoutine(deleteSingleton);
}

Basically, KStaticDeleter is replaced by a static function that is added
via qAddPostRoutine.

Alternatively, you can construct a singleton using a global static. But
the singleton isn't *the* global static:

struct KSingletonHolder
{
    KSingletonHolder() : singleton(0) { }
    ~KSingletonHolder() { delete singleton; singleton = 0; }
    void set(KMySingleton *ptr)
    { delete singleton; singleton = ptr; }

    KMySingleton *singleton;
};
K_GLOBAL_STATIC(KSingletonHolder, self)

Though this approach keeps a pointer to the pointer of the singleton object.
-- 
  Thiago Macieira  -  thiago (AT) macieira.info - thiago (AT) kde.org
    PGP/GPG: 0x6EF45358; fingerprint:
    E067 918B B660 DBD1 105C  966C 33F5 F005 6EF4 5358


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic