From 5ddc7b4578ddffb0ce99add81bca924099710926 Mon Sep 17 00:00:00 2001 From: hjxilinx Date: Thu, 9 Jan 2020 22:30:14 +0800 Subject: [PATCH] fix memory leaks and refactor some codes --- src/client/inc/tsclient.h | 31 ++++++++------- src/client/src/tscSecondaryMerge.c | 6 ++- src/client/src/tscUtil.c | 64 ++++++++++++++++++++---------- 3 files changed, 65 insertions(+), 36 deletions(-) diff --git a/src/client/inc/tsclient.h b/src/client/inc/tsclient.h index e21a7a4608..7e27031273 100644 --- a/src/client/inc/tsclient.h +++ b/src/client/inc/tsclient.h @@ -201,10 +201,10 @@ typedef struct SDataBlockList { } SDataBlockList; typedef struct SQueryInfo { - int16_t command; // the command may be different for each subclause, so keep it seperately. - uint16_t type; // query/insert/import type - char intervalTimeUnit; - + int16_t command; // the command may be different for each subclause, so keep it seperately. + uint16_t type; // query/insert/import type + char intervalTimeUnit; + int64_t etime, stime; int64_t nAggTimeInterval; // aggregation time interval int64_t nSlidingTime; // sliding window in mseconds @@ -221,9 +221,9 @@ typedef struct SQueryInfo { int16_t numOfTables; SMeterMetaInfo **pMeterInfo; struct STSBuf * tsBuf; - int64_t * defaultVal; // default value for interpolation - char * msg; // pointer to the pCmd->payload to keep error message temporarily - int64_t clauseLimit; // limit for this sub clause + int64_t * defaultVal; // default value for interpolation + char * msg; // pointer to the pCmd->payload to keep error message temporarily + int64_t clauseLimit; // limit for this sub clause } SQueryInfo; // data source from sql string or from file @@ -248,13 +248,13 @@ typedef struct { int32_t numOfTablesInSubmit; }; - int32_t clauseIndex; // index of multiple subclause query - int8_t isParseFinish; - short numOfCols; - - uint32_t allocSize; - char * payload; - int payloadLen; + int32_t clauseIndex; // index of multiple subclause query + int8_t isParseFinish; + short numOfCols; + uint32_t allocSize; + char * payload; + int payloadLen; + SQueryInfo **pQueryInfo; int32_t numOfClause; @@ -413,6 +413,7 @@ int32_t tscCreateResPointerInfo(SQueryInfo *pQueryInfo, SSqlRes *pRes); void tscDestroyResPointerInfo(SSqlRes *pRes); void tscFreeSqlCmdData(SSqlCmd *pCmd); +void tscFreeResData(SSqlObj* pSql); /** * only free part of resources allocated during query. @@ -435,7 +436,7 @@ void tscProcessMultiVnodesInsertFromFile(SSqlObj *pSql); void tscKillMetricQuery(SSqlObj *pSql); void tscInitResObjForLocalQuery(SSqlObj *pObj, int32_t numOfRes, int32_t rowLen); bool tscIsUpdateQuery(STscObj *pObj); -bool tscHasReachLimitation(SQueryInfo* pQueryInfo, SSqlRes* pRes); +bool tscHasReachLimitation(SQueryInfo *pQueryInfo, SSqlRes *pRes); char *tscGetErrorMsgPayload(SSqlCmd *pCmd); diff --git a/src/client/src/tscSecondaryMerge.c b/src/client/src/tscSecondaryMerge.c index e7a4941fc1..963c439d62 100644 --- a/src/client/src/tscSecondaryMerge.c +++ b/src/client/src/tscSecondaryMerge.c @@ -453,8 +453,12 @@ void tscDestroyLocalReducer(SSqlObj *pSql) { for(int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutputCols; ++i) { SQLFunctionCtx *pCtx = &pLocalReducer->pCtx[i]; tVariantDestroy(&pCtx->tag); + + if (pCtx->tagInfo.pTagCtxList != NULL) { + tfree(pCtx->tagInfo.pTagCtxList); + } } - + tfree(pLocalReducer->pCtx); } diff --git a/src/client/src/tscUtil.c b/src/client/src/tscUtil.c index 4596ae78fc..0ff801d08b 100644 --- a/src/client/src/tscUtil.c +++ b/src/client/src/tscUtil.c @@ -365,6 +365,39 @@ void tscFreeSqlCmdData(SSqlCmd* pCmd) { tscFreeSubqueryInfo(pCmd); } +void tscFreeResData(SSqlObj* pSql) { + SSqlRes* pRes = &pSql->res; + + tfree(pRes->pRsp); + pRes->row = 0; + + pRes->rspType = 0; + pRes->rspLen = 0; + pRes->row = 0; + + pRes->numOfRows = 0; + pRes->numOfTotal = 0; + pRes->numOfTotalInCurrentClause = 0; + + pRes->numOfGroups = 0; + pRes->precision = 0; + pRes->numOfnchar = 0; + pRes->qhandle = 0; + + pRes->offset = 0; + pRes->useconds = 0; + pRes->code = 0; + + pRes->data = NULL; + + tfree(pRes->pGroupRec); + + tscDestroyLocalReducer(pSql); + + tscDestroyResPointerInfo(pRes); + tfree(pRes->pColumnIndex); +} + void tscFreeSqlObjPartial(SSqlObj* pSql) { if (pSql == NULL || pSql->signature != pSql) { return; @@ -387,22 +420,11 @@ void tscFreeSqlObjPartial(SSqlObj* pSql) { pthread_mutex_lock(&pObj->mutex); tfree(pSql->sqlstr); pthread_mutex_unlock(&pObj->mutex); - - tfree(pRes->pRsp); - pRes->row = 0; - pRes->numOfRows = 0; - pRes->numOfTotal = 0; - pRes->numOfTotalInCurrentClause = 0; - - pRes->numOfGroups = 0; - tfree(pRes->pGroupRec); - - tscDestroyLocalReducer(pSql); - + + tscFreeResData(pSql); + tfree(pSql->pSubs); pSql->numOfSubs = 0; - tscDestroyResPointerInfo(pRes); - tfree(pRes->pColumnIndex); tscFreeSqlCmdData(pCmd); } @@ -2156,13 +2178,15 @@ void tscTryQueryNextClause(SSqlObj* pSql, void (*queryFp)()) { pSql->cmd.command = pQueryInfo->command; - pRes->numOfTotal += pRes->numOfTotalInCurrentClause; - pRes->numOfTotalInCurrentClause = 0; - pRes->rspType = 0; - - pSql->numOfSubs = 0; + //backup the total number of result first + int64_t num = pRes->numOfTotal + pRes->numOfTotalInCurrentClause; + tscFreeResData(pSql); + + pRes->numOfTotal = num; + tfree(pSql->pSubs); - + pSql->numOfSubs = 0; + if (pSql->fp != NULL) { pSql->fp = queryFp; assert(queryFp != NULL); -- GitLab