From 144405443793d90f4795f2844c30345c8e3a498a Mon Sep 17 00:00:00 2001 From: dapan1121 Date: Mon, 18 Jul 2022 18:57:47 +0800 Subject: [PATCH] fix: fix stmt memory leak --- source/client/src/clientStmt.c | 13 +++++++++---- source/libs/nodes/src/nodesUtilFuncs.c | 8 ++------ source/libs/parser/src/parInsert.c | 4 +++- source/libs/parser/src/parser.c | 6 ++++++ tests/script/api/batchprepare.c | 9 ++++----- 5 files changed, 24 insertions(+), 16 deletions(-) diff --git a/source/client/src/clientStmt.c b/source/client/src/clientStmt.c index bf00965c7a..7a83006961 100644 --- a/source/client/src/clientStmt.c +++ b/source/client/src/clientStmt.c @@ -6,11 +6,16 @@ #include "clientStmt.h" static int32_t stmtCreateRequest(STscStmt* pStmt) { + int32_t code = 0; + if (pStmt->exec.pRequest == NULL) { - return buildRequest(pStmt->taos->id, pStmt->sql.sqlStr, pStmt->sql.sqlLen, NULL, false, &pStmt->exec.pRequest); - } else { - return TSDB_CODE_SUCCESS; + code = buildRequest(pStmt->taos->id, pStmt->sql.sqlStr, pStmt->sql.sqlLen, NULL, false, &pStmt->exec.pRequest); + if (TSDB_CODE_SUCCESS == code) { + pStmt->exec.pRequest->syncQuery = true; + } } + + return code; } int32_t stmtSwitchStatus(STscStmt* pStmt, STMT_STATUS newStatus) { @@ -227,7 +232,7 @@ int32_t stmtParseSql(STscStmt* pStmt) { }; STMT_ERR_RET(stmtCreateRequest(pStmt)); - + STMT_ERR_RET(parseSql(pStmt->exec.pRequest, false, &pStmt->sql.pQuery, &stmtCb)); pStmt->bInfo.needParse = false; diff --git a/source/libs/nodes/src/nodesUtilFuncs.c b/source/libs/nodes/src/nodesUtilFuncs.c index 1dc3db033b..c96dc194ca 100644 --- a/source/libs/nodes/src/nodesUtilFuncs.c +++ b/source/libs/nodes/src/nodesUtilFuncs.c @@ -388,11 +388,6 @@ static void destroyDataSinkNode(SDataSinkNode* pNode) { nodesDestroyNode((SNode* static void destroyExprNode(SExprNode* pExpr) { taosArrayDestroy(pExpr->pAssociation); } -static void nodesDestroyNodePointer(void* node) { - SNode* pNode = *(SNode**)node; - nodesDestroyNode(pNode); -} - void nodesDestroyNode(SNode* pNode) { if (NULL == pNode) { return; @@ -716,6 +711,7 @@ void nodesDestroyNode(SNode* pNode) { case QUERY_NODE_QUERY: { SQuery* pQuery = (SQuery*)pNode; nodesDestroyNode(pQuery->pRoot); + nodesDestroyNode(pQuery->pPrepareRoot); taosMemoryFreeClear(pQuery->pResSchema); if (NULL != pQuery->pCmdMsg) { taosMemoryFreeClear(pQuery->pCmdMsg->pMsg); @@ -723,7 +719,7 @@ void nodesDestroyNode(SNode* pNode) { } taosArrayDestroy(pQuery->pDbList); taosArrayDestroy(pQuery->pTableList); - taosArrayDestroyEx(pQuery->pPlaceholderValues, nodesDestroyNodePointer); + taosArrayDestroy(pQuery->pPlaceholderValues); break; } case QUERY_NODE_LOGIC_PLAN_SCAN: { diff --git a/source/libs/parser/src/parInsert.c b/source/libs/parser/src/parInsert.c index 5ac4476d14..3defd0224b 100644 --- a/source/libs/parser/src/parInsert.c +++ b/source/libs/parser/src/parInsert.c @@ -1497,7 +1497,6 @@ static int32_t parseInsertBody(SInsertParseContext* pCxt) { memset(&pCxt->tags, 0, sizeof(pCxt->tags)); pCxt->pVgroupsHashObj = NULL; pCxt->pTableBlockHashObj = NULL; - pCxt->pTableMeta = NULL; return TSDB_CODE_SUCCESS; } @@ -1554,7 +1553,10 @@ int32_t parseInsertSql(SParseContext* pContext, SQuery** pQuery, SParseMetaCache if (NULL == *pQuery) { return TSDB_CODE_OUT_OF_MEMORY; } + } else { + nodesDestroyNode((*pQuery)->pRoot); } + (*pQuery)->execMode = QUERY_EXEC_MODE_SCHEDULE; (*pQuery)->haveResultSet = false; (*pQuery)->msgType = TDMT_VND_SUBMIT; diff --git a/source/libs/parser/src/parser.c b/source/libs/parser/src/parser.c index fdba0e2fcc..78d1e83436 100644 --- a/source/libs/parser/src/parser.c +++ b/source/libs/parser/src/parser.c @@ -82,11 +82,16 @@ static int32_t parseSqlSyntax(SParseContext* pCxt, SQuery** pQuery, SParseMetaCa } static int32_t setValueByBindParam(SValueNode* pVal, TAOS_MULTI_BIND* pParam) { + if (IS_VAR_DATA_TYPE(pVal->node.resType.type)) { + taosMemoryFreeClear(pVal->datum.p); + } + if (pParam->is_null && 1 == *(pParam->is_null)) { pVal->node.resType.type = TSDB_DATA_TYPE_NULL; pVal->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_NULL].bytes; return TSDB_CODE_SUCCESS; } + int32_t inputSize = (NULL != pParam->length ? *(pParam->length) : tDataTypes[pParam->buffer_type].bytes); pVal->node.resType.type = pParam->buffer_type; pVal->node.resType.bytes = inputSize; @@ -239,6 +244,7 @@ int32_t qStmtBindParams(SQuery* pQuery, TAOS_MULTI_BIND* pParams, int32_t colIdx } if (TSDB_CODE_SUCCESS == code && (colIdx < 0 || colIdx + 1 == pQuery->placeholderNum)) { + nodesDestroyNode(pQuery->pRoot); pQuery->pRoot = nodesCloneNode(pQuery->pPrepareRoot); if (NULL == pQuery->pRoot) { code = TSDB_CODE_OUT_OF_MEMORY; diff --git a/tests/script/api/batchprepare.c b/tests/script/api/batchprepare.c index 29c1fdb015..a330b6416e 100644 --- a/tests/script/api/batchprepare.c +++ b/tests/script/api/batchprepare.c @@ -218,7 +218,7 @@ typedef struct { } CaseCtrl; #if 0 -CaseCtrl gCaseCtrl = { // default +CaseCtrl gCaseCtrl = { .precision = TIME_PRECISION_MICRO, .bindNullNum = 0, .printCreateTblSql = false, @@ -251,7 +251,7 @@ CaseCtrl gCaseCtrl = { // default #if 1 -CaseCtrl gCaseCtrl = { +CaseCtrl gCaseCtrl = { // default .precision = TIME_PRECISION_MILLI, .bindNullNum = 0, .printCreateTblSql = false, @@ -2596,6 +2596,8 @@ void runAll(TAOS *taos) { printf("%s Begin\n", gCaseCtrl.caseCatalog); runCaseList(taos); +#if 0 + strcpy(gCaseCtrl.caseCatalog, "Micro DB precision Test"); printf("%s Begin\n", gCaseCtrl.caseCatalog); gCaseCtrl.precision = TIME_PRECISION_MICRO; @@ -2626,7 +2628,6 @@ void runAll(TAOS *taos) { runCaseList(taos); gCaseCtrl.bindRowNum = 0; -#if 0 strcpy(gCaseCtrl.caseCatalog, "Row Num Test"); printf("%s Begin\n", gCaseCtrl.caseCatalog); gCaseCtrl.rowNum = 1000; @@ -2640,7 +2641,6 @@ void runAll(TAOS *taos) { gCaseCtrl.runTimes = 2; runCaseList(taos); gCaseCtrl.runTimes = 0; -#endif strcpy(gCaseCtrl.caseCatalog, "Check Param Test"); printf("%s Begin\n", gCaseCtrl.caseCatalog); @@ -2648,7 +2648,6 @@ void runAll(TAOS *taos) { runCaseList(taos); gCaseCtrl.checkParamNum = false; -#if 0 strcpy(gCaseCtrl.caseCatalog, "Bind Col Num Test"); printf("%s Begin\n", gCaseCtrl.caseCatalog); gCaseCtrl.bindColNum = 6;