Fix a memory leak when creating Python3 modules. (#2019)

This commit is contained in:
Nils Berg 2019-12-11 15:01:45 +00:00 committed by Wenzel Jakob
parent dc9006db4f
commit 819802da99

View File

@ -794,11 +794,16 @@ public:
explicit module(const char *name, const char *doc = nullptr) { explicit module(const char *name, const char *doc = nullptr) {
if (!options::show_user_defined_docstrings()) doc = nullptr; if (!options::show_user_defined_docstrings()) doc = nullptr;
#if PY_MAJOR_VERSION >= 3 #if PY_MAJOR_VERSION >= 3
PyModuleDef *def = new PyModuleDef(); PyModuleDef *def = PyMem_New(PyModuleDef, 1);
std::memset(def, 0, sizeof(PyModuleDef)); std::memset(def, 0, sizeof(PyModuleDef));
def->m_name = name; def->m_name = name;
def->m_doc = doc; def->m_doc = doc;
def->m_size = -1; def->m_size = -1;
def->m_free = [](void* module ) {
if (module != nullptr) {
Py_XDECREF(PyModule_GetDef((PyObject*) module));
}
};
Py_INCREF(def); Py_INCREF(def);
m_ptr = PyModule_Create(def); m_ptr = PyModule_Create(def);
#else #else