From bee08c22c5dec4930ddcdac4d007e4b37b4ff09e Mon Sep 17 00:00:00 2001 From: kanliyong Date: Fri, 15 May 2020 15:14:00 +0800 Subject: [PATCH 1/2] memory leak when include Decimal python data type https://bugs.mysql.com/bug.php?id=99517&thanks=2¬ify=195 Description: Memory leak when inserting large amount of data using mysql-connector-python Memory grows consistently until out-of-memory In the end I found that this problem only occurs when the Decimal attribute is included --- src/mysql_capi_conversion.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/mysql_capi_conversion.c b/src/mysql_capi_conversion.c index 1692279..583e12a 100644 --- a/src/mysql_capi_conversion.c +++ b/src/mysql_capi_conversion.c @@ -701,8 +701,11 @@ PyObject* pytomy_decimal(PyObject *obj) { #ifdef PY3 - return PyBytes_FromString((const char *)PyUnicode_1BYTE_DATA( - PyObject_Str(obj))); + PyObject *str = PyObject_Str(obj); + PyObject *ret_tmp = (const char *)PyUnicode_1BYTE_DATA(str); + PyObject *ret = PyBytes_FromString(ret_tmp); + Py_DECREF(ret_tmp); + return ret; #else PyObject *numeric, *new_num; int tmp_size; From 1c1be1cc050321f2b4eb6d53d8302f94473288da Mon Sep 17 00:00:00 2001 From: kanliyong <840586247@qq.com> Date: Fri, 15 May 2020 16:39:21 +0800 Subject: [PATCH 2/2] Bug fix : memory leak when include Decimal python data --- src/mysql_capi_conversion.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/mysql_capi_conversion.c b/src/mysql_capi_conversion.c index 583e12a..7564e29 100644 --- a/src/mysql_capi_conversion.c +++ b/src/mysql_capi_conversion.c @@ -702,9 +702,8 @@ pytomy_decimal(PyObject *obj) { #ifdef PY3 PyObject *str = PyObject_Str(obj); - PyObject *ret_tmp = (const char *)PyUnicode_1BYTE_DATA(str); - PyObject *ret = PyBytes_FromString(ret_tmp); - Py_DECREF(ret_tmp); + PyObject *ret = PyBytes_FromString((const char *)PyUnicode_1BYTE_DATA(str)); + Py_DECREF(str); return ret; #else PyObject *numeric, *new_num;