Boost logo

Boost :

From: Jason Hise (chaos_at_[hidden])
Date: 2005-03-01 17:48:53

I just got a message from Michel Decima regarding an interesting
problem. What happens when you have to use a library which maintains
static or global variables that you don't have control over, and you
need your singletons to have access to them? The answer is that you
have to make sure that your singletons get destroyed before or as main
ends, not after. To do so, you have one of three options:

A) Use the dependency_lifetime and make sure not to keep any static or
global pointers.
B) Explicitly call destroy on each of your singletons before main exits.
C) Explicitly destroy the secret singleton
boost::singleton::detail::longevity_registry before main exits.

I recommend option three, combined with some RAII. For example:

struct CleanupSingletons ( )
    ~ CleanupSingletons ( )
       boost::singleton::detail::longevity_registry::pointer (
).destroy ( );

int main ( )
    CleanupSingletons cleanup;
    return 0;

I wonder if this functionality is useful enough that I should add a
non-member function 'cleanup' directly in the singleton namespace which
destroys the longevity_registry internally. Thoughts and opinions?


Boost list run by bdawes at, gregod at, cpdaniel at, john at