Skip to content

Commit 26a3349

Browse files
committed
avoid memory leak using NPY_NEEDS_INIT flag
1 parent 0dd1dfa commit 26a3349

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

stringdtype/stringdtype/src/dtype.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ new_stringdtype_instance(void)
1818
}
1919
new->base.elsize = sizeof(ss *);
2020
new->base.alignment = _Alignof(ss *);
21+
new->base.flags |= NPY_NEEDS_INIT;
2122

2223
return new;
2324
}
@@ -119,6 +120,12 @@ stringdtype_setitem(StringDTypeObject *descr, PyObject *obj, char **dataptr)
119120
PyErr_SetString(PyExc_MemoryError, "ssnewlen failed");
120121
return -1;
121122
}
123+
// the dtype instance has the NPY_NEEDS_INIT flag set,
124+
// so if *dataptr is NULL, that means we're initializing
125+
// the array and don't need to free an existing string
126+
if (*dataptr != NULL) {
127+
free((ss *)*dataptr);
128+
}
122129
*dataptr = (char *)str_val;
123130
Py_DECREF(val_obj);
124131
return 0;

0 commit comments

Comments
 (0)