Re: at_init / at_exit proposal

David Abrahams <[email protected]>
Newsgroups gmane.comp.lib.boost.langbinding
Message-ID <[email protected]>
Hi Dirk,

Dirk Gerrits <[email protected]> writes:

> Boost.Python currently uses some global/static variables that cause
> problems in embedding. (Basically you can't call Python's cleanup
> routines because the variables would be cleaned up after Python
> itself, causing a crash.)
>
> I propose to add a mechanism like the following which I hope will help
> to fix this issue:
>
> struct at_init
> {
>      explicit at_init(function<void ()> f)
>      {
>          if (extending)
>              f();
>          else
>              /* register f somewhere to be called after
>              the interpreter starts */
>      }
> };

I can't imagine what we'd want this for.  Module import time is early
enough to initialize any state associated with a module, and the rest
of the state can safely live forever.

> struct at_exit
> {
>      function<void ()> m_f;
>
>      explicit at_exit(function<void ()> f)
>          : m_f(f)
>      {
>          if (embedding)
>              /* register f somewhere to be called before the
>              interpreter shuts down */
>      }
>
>      ~at_exit()
>      {
>          if (extending)
>              m_f();
>      }
> };

I don't understand the logic here.  Why would we want different
behavior for extending and embedding?

> Example usage:
> namespace {
>
> registry_t* registry;
> void create() { registry = new registry_t;
>                  /* initialization */ }
> void destroy() { delete registry; }
> at_init registry_creation(&create);
> at_exit registry_destruction(&destroy);
>
> registry_t& entries() { return *registry; }
> ..
> } // unnamed
>
> This will allow things to be done at interpreter startup and shutdown,
> like the conversion registry initialization and shutdown above.
>
> When extending, this is the same as the 'startup' and 'shutdown' of
> the translation unit.

I don't think we need to tear down the entire registry at shutdown.
It seems enough to reset all of the object()s and handle<>s it's
holding.


-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.