Skip to content

Add note about module destructors #733

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions docs/advanced/misc.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
=====================================

Expand Down