[C++-sig] Re: module unloading callback?

Niall Douglas s_sourceforge at nedprod.com
Tue Jan 20 00:00:41 CET 2004


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 19 Jan 2004 at 13:12, Mike Rovner wrote:

> Daniel Holth wrote:
> > I've been wrapping a library thats documentation says you should
> > init() and free() some internal data structures before and after you
> > use it.  I can put code in BOOST_MODULE_INIT to call init, but where
> > would I put the tear-down code?  Thanks.
> 
> That is very common problem for many libraries. They usually provide
>   libInit(...)
>   libFinish(...)
> 
> My approach is simply wrap them as (module) lib functions:
>   Init(...) and Finish(...)
> for the user to call manually.
> 
> Optionaly as you said, 'init' is called automatically on module init.
> That is really depends on library usage model. But because python
> _never_ unloads its modules you can't put 'finish' call anywere to be
> called automatically.

You can force it to do so via C and if you're careful it seems to run 
just fine. Just don't refer to the no longer existing module :)

To have something called on python exit eg; if python wants to exit 
the process, use:

	def("DeinitTnFOX", &DeinitialiseTnFOXPython);
	PyRun_SimpleString("import atexit\n"
						   "def Internal_CallDeinitTnFOX():\n"
						   "    DeinitTnFOX()\n"
						   "atexit.register(Internal_CallDeinitTnFOX)\n");

This calls DeinitTnFOX() on exit.

Cheers,
Niall





-----BEGIN PGP SIGNATURE-----
Version: idw's PGP-Frontend 4.9.6.1 / 9-2003 + PGP 8.0.2

iQA/AwUBQAxhmcEcvDLFGKbPEQLH1gCbBRdDz0uaF6As6qej1FZsrMNtHsAAmQG1
SWib0ZXNUHScXOQijfLEfeUk
=QlEs
-----END PGP SIGNATURE-----




More information about the Cplusplus-sig mailing list