diff --git a/src/client/src/tscPrepare.c b/src/client/src/tscPrepare.c index 1d569064998583f5f975deec270431ef187adbcc..6ade48cb196b2e50fd9ba8f811f0a7e9a8930484 100644 --- a/src/client/src/tscPrepare.c +++ b/src/client/src/tscPrepare.c @@ -1522,6 +1522,7 @@ TAOS_STMT* taos_stmt_init(TAOS* taos) { pSql->isBind = true; pStmt->pSql = pSql; pStmt->last = STMT_INIT; + registerSqlObj(pSql); return pStmt; } @@ -1783,7 +1784,9 @@ int taos_stmt_set_tbname(TAOS_STMT* stmt, const char* name) { int taos_stmt_close(TAOS_STMT* stmt) { STscStmt* pStmt = (STscStmt*)stmt; - STMT_CHECK + if (pStmt == NULL || pStmt->taos == NULL) { + STMT_RET(TSDB_CODE_TSC_DISCONNECTED); + } if (!pStmt->isInsert) { SNormalStmt* normal = &pStmt->normal; if (normal->params != NULL) { @@ -1805,14 +1808,15 @@ int taos_stmt_close(TAOS_STMT* stmt) { pStmt->mtb.pTableBlockHashList = tscDestroyBlockHashTable(pStmt->mtb.pTableBlockHashList, rmMeta); if (pStmt->pSql){ taosHashCleanup(pStmt->pSql->cmd.insertParam.pTableBlockHashList); + pStmt->pSql->cmd.insertParam.pTableBlockHashList = NULL; } - pStmt->pSql->cmd.insertParam.pTableBlockHashList = NULL; + taosArrayDestroy(pStmt->mtb.tags); tfree(pStmt->mtb.sqlstr); } } - tscFreeSqlObj(pStmt->pSql); + taos_free_result(pStmt->pSql); tfree(pStmt); STMT_RET(TSDB_CODE_SUCCESS); } @@ -1960,11 +1964,7 @@ int taos_stmt_execute(TAOS_STMT* stmt) { if (sql == NULL) { ret = TSDB_CODE_TSC_OUT_OF_MEMORY; } else { - if (pStmt->pSql != NULL) { - tscFreeSqlObj(pStmt->pSql); - pStmt->pSql = NULL; - } - + taosReleaseRef(tscObjRef, pStmt->pSql->self); pStmt->pSql = taos_query((TAOS*)pStmt->taos, sql); ret = taos_errno(pStmt->pSql); free(sql); @@ -1986,6 +1986,7 @@ TAOS_RES *taos_stmt_use_result(TAOS_STMT* stmt) { return NULL; } TAOS_RES* result = pStmt->pSql; + pStmt->pSql = NULL; return result; } diff --git a/tests/script/api/stmt.c b/tests/script/api/stmt.c index 168d281f7062ae892643cd61c53e0448106201b7..baf40c1421df1de4afcc8570288f642df067130a 100644 --- a/tests/script/api/stmt.c +++ b/tests/script/api/stmt.c @@ -465,6 +465,7 @@ void taos_stmt_use_result_query(void *taos, char *col, int type) { TAOS_RES *result = taos_stmt_use_result(stmt); assert(result != NULL); print_result(result); + taos_free_result(result); assert(taos_stmt_close(stmt) == 0); free(params); free(stmt_sql);