Skip to content

Commit da5a779

Browse files
authored
Merge pull request numpy#26586 from ngoldbaum/fix-stringdtype-memoryleaks
BUG: Fix memory leaks found by valgrind
2 parents 2519e34 + edb449d commit da5a779

File tree

3 files changed

+5
-2
lines changed

3 files changed

+5
-2
lines changed

numpy/_core/src/multiarray/arraytypes.c.src

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4222,6 +4222,7 @@ NPY_NO_EXPORT PyArray_Descr *
42224222
PyArray_DescrFromType(int type)
42234223
{
42244224
PyArray_Descr *ret = NULL;
4225+
npy_bool is_stringdtype = (type == NPY_VSTRING || type == NPY_VSTRINGLTR);
42254226

42264227
if (type < 0) {
42274228
/*
@@ -4233,7 +4234,7 @@ PyArray_DescrFromType(int type)
42334234
*/
42344235
ret = NULL;
42354236
}
4236-
else if (type == NPY_VSTRING || type == NPY_VSTRINGLTR) {
4237+
else if (is_stringdtype) {
42374238
ret = (PyArray_Descr *)new_stringdtype_instance(NULL, 1);
42384239
}
42394240
// builtin legacy dtypes
@@ -4280,7 +4281,7 @@ PyArray_DescrFromType(int type)
42804281
PyErr_SetString(PyExc_ValueError,
42814282
"Invalid data-type for array");
42824283
}
4283-
else {
4284+
else if (!is_stringdtype) {
42844285
Py_INCREF(ret);
42854286
}
42864287

numpy/_core/src/multiarray/descriptor.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2025,6 +2025,7 @@ arraydescr_dealloc(PyArray_Descr *self)
20252025
{
20262026
Py_XDECREF(self->typeobj);
20272027
if (!PyDataType_ISLEGACY(self)) {
2028+
Py_TYPE(self)->tp_free((PyObject *)self);
20282029
return;
20292030
}
20302031
_PyArray_LegacyDescr *lself = (_PyArray_LegacyDescr *)self;

numpy/_core/src/multiarray/stringdtype/casts.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,7 @@ string_to_pyfloat(char *in, int has_null,
894894
goto fail; \
895895
} \
896896
double dval = PyFloat_AS_DOUBLE(pyfloat_value); \
897+
Py_DECREF(pyfloat_value); \
897898
npy_##typename fval = (double_to_float)(dval); \
898899
\
899900
if (NPY_UNLIKELY(isinf_name(fval) && !(npy_isinf(dval)))) { \

0 commit comments

Comments
 (0)