Closed lsix closed 2 years ago
You'll need to merge with head for this and your other PRs. I've also started cleanup with using std::string for all strings.
Hi @schwehr,
I have rebased this branch onto master.
I have also rephrased the commit message (and PR comment). When re reading the original one, I did not find it very clear. I hope it now is easier to understand.
All insertions into python dictionaries are done using PyDict_SetItem. This function requires a PyObject* as the KEY (second) argument.
In few places a temporary PyObject* is created when inserting in the dict but is not cleaned (i.e Py_DECREF'ed). This is done like so:
This allocates a PyObject* from the "cargos" string, and gives it directly to PyDict_SetItem. The problem is the following:
When the containing dict is eventually deleted, its reference to the key object is decremented, leaving its refcount to 1. This leads to a leak of the key object.
The correct usage would have been to use:
This use case is quite common, leading cpython to provide a helper function that does just that: PyDict_SetItemString. This function takes a const char * argument as KEY argument and handles the refcounting properly. The previous example then becomes:
This patch proposes to use PyDict_SetItemString instead of PyDict_SetItem wherever possible.