Skip to content

Commit 16c28fa

Browse files
authored
Merge pull request #136 from martin-belanger/memleak-swig-module
Fix memory leak in SWIG module
2 parents 9b3ddf5 + ae234c5 commit 16c28fa

1 file changed

Lines changed: 18 additions & 11 deletions

File tree

libnvme/nvme.i

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,13 @@ static int discover_err = 0;
161161
if ($1) free($1);
162162
}
163163

164+
%{
165+
static void PyDict_SetItemStringDecRef(PyObject *p, const char *key, PyObject *val) {
166+
PyDict_SetItemString(p, key, val); /* Does NOT steal reference to val .. */
167+
Py_XDECREF(val); /* .. therefore decrement ref. count. */
168+
}
169+
%}
170+
164171
%typemap(out) struct nvmf_discovery_log * {
165172
struct nvmf_discovery_log *log = $1;
166173
int numrec = log? log->numrec : 0, i;
@@ -190,7 +197,7 @@ static int discover_err = 0;
190197
default:
191198
val = PyLong_FromLong(e->trtype);
192199
}
193-
PyDict_SetItemString(entry, "trtype", val);
200+
PyDict_SetItemStringDecRef(entry, "trtype", val);
194201
switch (e->adrfam) {
195202
case NVMF_ADDR_FAMILY_PCI:
196203
val = PyUnicode_FromString("pci");
@@ -210,13 +217,13 @@ static int discover_err = 0;
210217
default:
211218
val = PyLong_FromLong(e->adrfam);
212219
}
213-
PyDict_SetItemString(entry, "adrfam", val);
220+
PyDict_SetItemStringDecRef(entry, "adrfam", val);
214221
val = PyUnicode_FromString(e->traddr);
215-
PyDict_SetItemString(entry, "traddr", val);
222+
PyDict_SetItemStringDecRef(entry, "traddr", val);
216223
val = PyUnicode_FromString(e->trsvcid);
217-
PyDict_SetItemString(entry, "trsvcid", val);
224+
PyDict_SetItemStringDecRef(entry, "trsvcid", val);
218225
val = PyUnicode_FromString(e->subnqn);
219-
PyDict_SetItemString(entry, "subnqn", val);
226+
PyDict_SetItemStringDecRef(entry, "subnqn", val);
220227
switch (e->subtype) {
221228
case NVME_NQN_DISC:
222229
val = PyUnicode_FromString("referral");
@@ -230,7 +237,7 @@ static int discover_err = 0;
230237
default:
231238
val = PyLong_FromLong(e->subtype);
232239
}
233-
PyDict_SetItemString(entry, "subtype", val);
240+
PyDict_SetItemStringDecRef(entry, "subtype", val);
234241
switch (e->treq) {
235242
case NVMF_TREQ_NOT_SPECIFIED:
236243
val = PyUnicode_FromString("not specified");
@@ -247,14 +254,14 @@ static int discover_err = 0;
247254
default:
248255
val = PyLong_FromLong(e->treq);
249256
}
250-
PyDict_SetItemString(entry, "treq", val);
257+
PyDict_SetItemStringDecRef(entry, "treq", val);
251258
val = PyLong_FromLong(e->portid);
252-
PyDict_SetItemString(entry, "portid", val);
259+
PyDict_SetItemStringDecRef(entry, "portid", val);
253260
val = PyLong_FromLong(e->cntlid);
254-
PyDict_SetItemString(entry, "cntlid", val);
261+
PyDict_SetItemStringDecRef(entry, "cntlid", val);
255262
val = PyLong_FromLong(e->asqsz);
256-
PyDict_SetItemString(entry, "asqsz", val);
257-
PyList_SetItem(obj, i, entry);
263+
PyDict_SetItemStringDecRef(entry, "asqsz", val);
264+
PyList_SetItem(obj, i, entry); /* steals ref. to entry */
258265
}
259266
$result = obj;
260267
};

0 commit comments

Comments
 (0)