Skip to content

Commit ff8f7db

Browse files
committed
Fix a memory leak when creating Python3 modules.
1 parent fb910ae commit ff8f7db

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

include/pybind11/pybind11.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,11 @@ class module : public object {
799799
def->m_name = name;
800800
def->m_doc = doc;
801801
def->m_size = -1;
802-
Py_INCREF(def);
802+
def->m_free = [](void* module ) {
803+
if (module != NULL) {
804+
delete PyModule_GetDef((PyObject*) module);
805+
}
806+
};
803807
m_ptr = PyModule_Create(def);
804808
#else
805809
m_ptr = Py_InitModule3(name, nullptr, doc);

0 commit comments

Comments
 (0)