Skip to content

Commit 31fd611

Browse files
committed
Don't leak when PyDict_SetItemString called
1 parent b685765 commit 31fd611

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

softioc/extension.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,16 @@
1818
#define PyInt_FromLong(value) PyLong_FromLong(value)
1919
#endif
2020

21+
/* Reference stealing version of PyDict_SetItemString */
22+
static void set_dict_item_steal(PyObject *dict, const char *name, PyObject *py_value)
23+
{
24+
PyDict_SetItemString(dict, name, py_value);
25+
Py_DECREF(py_value);
26+
}
27+
2128
/* Helper for function below. */
2229
#define ADD_ENUM(dict, name) \
23-
PyDict_SetItemString(dict, #name, PyInt_FromLong(name))
30+
set_dict_item_steal(dict, #name, PyInt_FromLong(name))
2431

2532
/* Alas, EPICS has changed the numerical assignments of the DBF_ enums between
2633
* versions, so to avoid unpleasant surprises, we compute thes values here in C
@@ -76,7 +83,7 @@ static PyObject *get_field_offsets(PyObject *self, PyObject *args)
7683
dbentry.pflddes->offset,
7784
dbentry.pflddes->size,
7885
dbentry.pflddes->field_type);
79-
PyDict_SetItemString(dict, field_name, ost);
86+
set_dict_item_steal(dict, field_name, ost);
8087
status = dbNextField(&dbentry, 0);
8188
}
8289

0 commit comments

Comments
 (0)