Fix for CONPY-119: Fixed memory leak

When creating a cursor with result set type named_tuple
or dictionary, references were not decremented correctly.

For named tuples we don't use a static variable anymore, instead
of it will be created by PyStructSequence_NewType.
This commit is contained in:
Georg Richter
2020-10-03 16:41:29 +02:00
parent cbd51decc6
commit 846c0d0fdb
3 changed files with 7 additions and 8 deletions

View File

@ -307,7 +307,6 @@ extern PyObject *Mariadb_ProgrammingError;
extern PyObject *Mariadb_NotSupportedError;
extern PyObject *Mariadb_Warning;
extern PyObject *Mrdb_Pickle;
extern PyObject *decimal_module,
*decimal_type;

View File

@ -26,7 +26,6 @@
extern int codecs_datetime_init(void);
PyObject *Mrdb_Pickle= NULL;
PyObject *cnx_pool= NULL;
PyObject *decimal_module= NULL,
*decimal_type= NULL;
@ -165,9 +164,6 @@ PyMODINIT_FUNC PyInit__mariadb(void)
goto error;
}
/* we need pickle for object serialization */
Mrdb_Pickle= PyImport_ImportModule("pickle");
Py_TYPE(&MrdbCursor_Type) = &PyType_Type;
if (PyType_Ready(&MrdbCursor_Type) == -1)
{

View File

@ -438,7 +438,11 @@ void MrdbCursor_clear(MrdbCursor *self, uint8_t new_stmt)
}
self->fetched= 0;
MARIADB_FREE_MEM(self->sequence_fields);
if (self->sequence_fields)
{
MARIADB_FREE_MEM(self->sequence_fields);
Py_DECREF((PyObject *)self->sequence_type);
}
self->fields= NULL;
self->row_count= 0;
self->affected_rows= 0;
@ -459,6 +463,7 @@ static void ma_set_result_column_value(MrdbCursor *self, PyObject *row, uint32_t
break;
case RESULT_DICTIONARY:
PyDict_SetItemString(row, self->fields[column].name, self->values[column]);
Py_DECREF(self->values[column]); /* CONPY-119 */
break;
default:
PyTuple_SET_ITEM(row, column, (self)->values[column]);
@ -561,8 +566,7 @@ static int Mrdb_GetFieldInfo(MrdbCursor *self)
{
self->sequence_fields[i].name= self->fields[i].name;
}
self->sequence_type= PyMem_RawCalloc(1,sizeof(PyTypeObject));
PyStructSequence_InitType(self->sequence_type, &self->sequence_desc);
self->sequence_type= PyStructSequence_NewType(&self->sequence_desc);
}
}
return 0;