Skip to content

Commit

Permalink
Fix for CONPY-119: Fixed memory leak
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
9EOR9 committed Oct 3, 2020
1 parent cbd51de commit 846c0d0
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 8 deletions.
1 change: 0 additions & 1 deletion include/mariadb_python.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 0 additions & 4 deletions mariadb/mariadb.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
{
Expand Down
10 changes: 7 additions & 3 deletions mariadb/mariadb_cursor.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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]);
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 846c0d0

Please sign in to comment.