From 56adbe7361e421fba5f0c72c233827d39a550507 Mon Sep 17 00:00:00 2001 From: Dustin Spicuzza Date: Wed, 15 Mar 2017 22:06:25 -0400 Subject: [PATCH] Add note about module destructors - Technique provided by @jagerman - Fixes #638 --- docs/advanced/misc.rst | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/docs/advanced/misc.rst b/docs/advanced/misc.rst index 163fb0ee97..92d77134af 100644 --- a/docs/advanced/misc.rst +++ b/docs/advanced/misc.rst @@ -171,6 +171,26 @@ would be then able to access the data behind the same pointer. .. [#f6] https://docs.python.org/3/extending/extending.html#using-capsules +Module Destructors +================== + +pybind11 does not explicitly provide a mechanism that can be called at module +destruction time (and most users will not need this functionality). However, +if you do happen to need the ability to do something when a module is +destroyed, you can use a Python capsule to accomplish this: + +.. code-block:: cpp + + // the capsule needs something to reference + static int unused; + + py::capsule cleanup(&unused, [](PyObject * capsule) { + // do cleanup here -- this function is called with the GIL held + }); + + m.add_object("_cleanup", cleanup); + + Generating documentation using Sphinx =====================================