paulross/PythonExtensionPatterns

Some Memory Leaks

ldo opened this issue · 1 comments

ldo commented

I see a couple of concerns in your section on Getting and Setting Module Globals.

In a statement like

if (PyModule_AddObject(m, NAME_TUP, Py_BuildValue("iii", 66, 68, 73))) {
    goto except;

the doc says that the reference to the object is only stolen on success. So if the PyModule_AddObject call fails, then the object created by the Py_BuildValue call in this case will be leaked.

Further down that page, the calls to PyDict_SetItem (e.g. PyDict_SetItem(pMap, PyBytes_FromString("123"), PyLong_FromLong(123))) leak references to the key and value objects.

True. If PyModule_AddObject fails there will be memory leak of Py_BuildValue etc. However then the module initialisation will fail so memory leaks are likely a minor issue. Of course you could clean up the (successful) objects in the (failed) module a the expense of more code.