提交 78f58194 编写于 作者: H Haojun Liao

[td-5404]<fix>: fix memory leak in union query.

上级 b4154d8a
...@@ -8005,6 +8005,28 @@ static void freeElem(void* p) { ...@@ -8005,6 +8005,28 @@ static void freeElem(void* p) {
tfree(*(char**)p); tfree(*(char**)p);
} }
int32_t tnameComparFn(const void* p1, const void* p2) {
SName* pn1 = (SName*)p1;
SName* pn2 = (SName*)p2;
int32_t ret = strncmp(pn1->acctId, pn2->acctId, tListLen(pn1->acctId));
if (ret != 0) {
return ret > 0? 1:-1;
} else {
ret = strncmp(pn1->dbname, pn2->dbname, tListLen(pn1->dbname));
if (ret != 0) {
return ret > 0? 1:-1;
} else {
ret = strncmp(pn1->tname, pn2->tname, tListLen(pn1->tname));
if (ret != 0) {
return ret > 0? 1:-1;
} else {
return 0;
}
}
}
}
int32_t loadAllTableMeta(SSqlObj* pSql, struct SSqlInfo* pInfo) { int32_t loadAllTableMeta(SSqlObj* pSql, struct SSqlInfo* pInfo) {
SSqlCmd* pCmd = &pSql->cmd; SSqlCmd* pCmd = &pSql->cmd;
...@@ -8055,6 +8077,9 @@ int32_t loadAllTableMeta(SSqlObj* pSql, struct SSqlInfo* pInfo) { ...@@ -8055,6 +8077,9 @@ int32_t loadAllTableMeta(SSqlObj* pSql, struct SSqlInfo* pInfo) {
plist = taosArrayInit(4, POINTER_BYTES); plist = taosArrayInit(4, POINTER_BYTES);
pVgroupList = taosArrayInit(4, POINTER_BYTES); pVgroupList = taosArrayInit(4, POINTER_BYTES);
taosArraySort(tableNameList, tnameComparFn);
taosArrayRemoveDuplicate(tableNameList, tnameComparFn, NULL);
size_t numOfTables = taosArrayGetSize(tableNameList); size_t numOfTables = taosArrayGetSize(tableNameList);
for (int32_t i = 0; i < numOfTables; ++i) { for (int32_t i = 0; i < numOfTables; ++i) {
SName* pname = taosArrayGet(tableNameList, i); SName* pname = taosArrayGet(tableNameList, i);
...@@ -8075,8 +8100,7 @@ int32_t loadAllTableMeta(SSqlObj* pSql, struct SSqlInfo* pInfo) { ...@@ -8075,8 +8100,7 @@ int32_t loadAllTableMeta(SSqlObj* pSql, struct SSqlInfo* pInfo) {
continue; continue;
} }
} else if (pTableMeta->tableType == TSDB_SUPER_TABLE) { } else if (pTableMeta->tableType == TSDB_SUPER_TABLE) {
// the vgroup list of a super table is not kept in local buffer, so here need retrieve it // the vgroup list of super table is not kept in local buffer, so here need retrieve it from the mnode each time
// from the mnode each time
char* t = strdup(name); char* t = strdup(name);
taosArrayPush(pVgroupList, &t); taosArrayPush(pVgroupList, &t);
} }
...@@ -8103,6 +8127,7 @@ int32_t loadAllTableMeta(SSqlObj* pSql, struct SSqlInfo* pInfo) { ...@@ -8103,6 +8127,7 @@ int32_t loadAllTableMeta(SSqlObj* pSql, struct SSqlInfo* pInfo) {
if (pInfo->funcs) { if (pInfo->funcs) {
funcSize = taosArrayGetSize(pInfo->funcs); funcSize = taosArrayGetSize(pInfo->funcs);
} }
if (funcSize > 0) { if (funcSize > 0) {
for (size_t i = 0; i < funcSize; ++i) { for (size_t i = 0; i < funcSize; ++i) {
SStrToken* t = taosArrayGet(pInfo->funcs, i); SStrToken* t = taosArrayGet(pInfo->funcs, i);
......
...@@ -1315,8 +1315,7 @@ void tscFreeQueryInfo(SSqlCmd* pCmd, bool removeMeta) { ...@@ -1315,8 +1315,7 @@ void tscFreeQueryInfo(SSqlCmd* pCmd, bool removeMeta) {
return; return;
} }
SQueryInfo* pQueryInfo = tscGetQueryInfo(pCmd); SQueryInfo* pQueryInfo = pCmd->pQueryInfo;
while(pQueryInfo != NULL) { while(pQueryInfo != NULL) {
SQueryInfo* p = pQueryInfo->sibling; SQueryInfo* p = pQueryInfo->sibling;
...@@ -1596,7 +1595,7 @@ void freeUdfInfo(SUdfInfo* pUdfInfo) { ...@@ -1596,7 +1595,7 @@ void freeUdfInfo(SUdfInfo* pUdfInfo) {
taosCloseDll(pUdfInfo->handle); taosCloseDll(pUdfInfo->handle);
} }
// todo refactor
void* tscDestroyUdfArrayList(SArray* pUdfList) { void* tscDestroyUdfArrayList(SArray* pUdfList) {
if (pUdfList == NULL) { if (pUdfList == NULL) {
return NULL; return NULL;
...@@ -2986,6 +2985,7 @@ void tscInitQueryInfo(SQueryInfo* pQueryInfo) { ...@@ -2986,6 +2985,7 @@ void tscInitQueryInfo(SQueryInfo* pQueryInfo) {
int32_t tscAddQueryInfo(SSqlCmd* pCmd) { int32_t tscAddQueryInfo(SSqlCmd* pCmd) {
assert(pCmd != NULL); assert(pCmd != NULL);
SQueryInfo* pQueryInfo = calloc(1, sizeof(SQueryInfo)); SQueryInfo* pQueryInfo = calloc(1, sizeof(SQueryInfo));
if (pQueryInfo == NULL) { if (pQueryInfo == NULL) {
return TSDB_CODE_TSC_OUT_OF_MEMORY; return TSDB_CODE_TSC_OUT_OF_MEMORY;
} }
...@@ -3968,17 +3968,21 @@ void tscTryQueryNextClause(SSqlObj* pSql, __async_cb_func_t fp) { ...@@ -3968,17 +3968,21 @@ void tscTryQueryNextClause(SSqlObj* pSql, __async_cb_func_t fp) {
//backup the total number of result first //backup the total number of result first
int64_t num = pRes->numOfTotal + pRes->numOfClauseTotal; int64_t num = pRes->numOfTotal + pRes->numOfClauseTotal;
// DON't free final since it may be recoreded and used later in APP // DON't free final since it may be recoreded and used later in APP
TAOS_FIELD* finalBk = pRes->final; TAOS_FIELD* finalBk = pRes->final;
pRes->final = NULL; pRes->final = NULL;
tscFreeSqlResult(pSql); tscFreeSqlResult(pSql);
pRes->final = finalBk; pRes->final = finalBk;
pRes->numOfTotal = num; pRes->numOfTotal = num;
for(int32_t i = 0; i < pSql->subState.numOfSub; ++i) {
taos_free_result(pSql->pSubs[i]);
}
tfree(pSql->pSubs); tfree(pSql->pSubs);
pSql->subState.numOfSub = 0; pSql->subState.numOfSub = 0;
pSql->fp = fp; pSql->fp = fp;
tscDebug("0x%"PRIx64" try data in the next subclause", pSql->self); tscDebug("0x%"PRIx64" try data in the next subclause", pSql->self);
...@@ -4618,6 +4622,20 @@ int32_t nameComparFn(const void* n1, const void* n2) { ...@@ -4618,6 +4622,20 @@ int32_t nameComparFn(const void* n1, const void* n2) {
} }
} }
static void freeContent(void* p) {
char* ptr = *(char**)p;
tfree(ptr);
}
static int32_t contCompare(const void* p1, const void* p2) {
int32_t ret = strcmp(p1, p2);
if (ret == 0) {
return 0;
} else {
return ret > 0 ? 1:-1;
}
}
int tscTransferTableNameList(SSqlObj *pSql, const char *pNameList, int32_t length, SArray* pNameArray) { int tscTransferTableNameList(SSqlObj *pSql, const char *pNameList, int32_t length, SArray* pNameArray) {
SSqlCmd *pCmd = &pSql->cmd; SSqlCmd *pCmd = &pSql->cmd;
...@@ -4665,32 +4683,7 @@ int tscTransferTableNameList(SSqlObj *pSql, const char *pNameList, int32_t lengt ...@@ -4665,32 +4683,7 @@ int tscTransferTableNameList(SSqlObj *pSql, const char *pNameList, int32_t lengt
} }
taosArraySort(pNameArray, nameComparFn); taosArraySort(pNameArray, nameComparFn);
taosArrayRemoveDuplicate(pNameArray, contCompare, freeContent);
int32_t pos = 0;
for(int32_t i = 1; i < len; ++i) {
char** p1 = taosArrayGet(pNameArray, pos);
char** p2 = taosArrayGet(pNameArray, i);
if (strcmp(*p1, *p2) == 0) {
// do nothing
} else {
if (pos + 1 != i) {
char* p = taosArrayGetP(pNameArray, pos + 1);
tfree(p);
taosArraySet(pNameArray, pos + 1, p2);
pos += 1;
} else {
pos += 1;
}
}
}
for(int32_t i = pos + 1; i < pNameArray->size; ++i) {
char* p = taosArrayGetP(pNameArray, i);
tfree(p);
}
pNameArray->size = pos + 1;
return TSDB_CODE_SUCCESS; return TSDB_CODE_SUCCESS;
} }
......
...@@ -11,7 +11,7 @@ TARGET_LINK_LIBRARIES(tutil pthread os lz4 z rmonotonic) ...@@ -11,7 +11,7 @@ TARGET_LINK_LIBRARIES(tutil pthread os lz4 z rmonotonic)
IF (TD_LINUX) IF (TD_LINUX)
TARGET_LINK_LIBRARIES(tutil m rt) TARGET_LINK_LIBRARIES(tutil m rt)
# ADD_SUBDIRECTORY(tests) ADD_SUBDIRECTORY(tests)
FIND_PATH(ICONV_INCLUDE_EXIST iconv.h /usr/include/ /usr/local/include/) FIND_PATH(ICONV_INCLUDE_EXIST iconv.h /usr/include/ /usr/local/include/)
IF (ICONV_INCLUDE_EXIST) IF (ICONV_INCLUDE_EXIST)
......
...@@ -60,6 +60,13 @@ void *taosArrayAddBatch(SArray *pArray, const void *pData, int nEles); ...@@ -60,6 +60,13 @@ void *taosArrayAddBatch(SArray *pArray, const void *pData, int nEles);
*/ */
void taosArrayRemoveBatch(SArray *pArray, const int32_t* pData, int32_t numOfElems); void taosArrayRemoveBatch(SArray *pArray, const int32_t* pData, int32_t numOfElems);
/**
*
* @param pArray
* @param comparFn
* @param fp
*/
void taosArrayRemoveDuplicate(SArray *pArray, __compar_fn_t comparFn, void (*fp)(void*));
/** /**
* add all element from the source array list into the destination * add all element from the source array list into the destination
......
...@@ -124,6 +124,46 @@ void taosArrayRemoveBatch(SArray *pArray, const int32_t* pData, int32_t numOfEle ...@@ -124,6 +124,46 @@ void taosArrayRemoveBatch(SArray *pArray, const int32_t* pData, int32_t numOfEle
pArray->size -= numOfElems; pArray->size -= numOfElems;
} }
void taosArrayRemoveDuplicate(SArray *pArray, __compar_fn_t comparFn, void (*fp)(void*)) {
assert(pArray);
size_t size = pArray->size;
if (size <= 1) {
return;
}
int32_t pos = 0;
for(int32_t i = 1; i < size; ++i) {
char* p1 = taosArrayGet(pArray, pos);
char* p2 = taosArrayGet(pArray, i);
if (comparFn(p1, p2) == 0) {
// do nothing
} else {
if (pos + 1 != i) {
void* p = taosArrayGet(pArray, pos + 1);
if (fp != NULL) {
fp(p);
}
taosArraySet(pArray, pos + 1, p2);
pos += 1;
} else {
pos += 1;
}
}
}
if (fp != NULL) {
for(int32_t i = pos + 1; i < pArray->size; ++i) {
void* p = taosArrayGet(pArray, i);
fp(p);
}
}
pArray->size = pos + 1;
}
void* taosArrayAddAll(SArray* pArray, const SArray* pInput) { void* taosArrayAddAll(SArray* pArray, const SArray* pInput) {
return taosArrayAddBatch(pArray, pInput->pData, (int32_t) taosArrayGetSize(pInput)); return taosArrayAddBatch(pArray, pInput->pData, (int32_t) taosArrayGetSize(pInput));
} }
......
...@@ -5,6 +5,7 @@ echo -n "" > /tmp/empty ...@@ -5,6 +5,7 @@ echo -n "" > /tmp/empty
dd if=/dev/zero bs=3584 of=/tmp/big count=1 dd if=/dev/zero bs=3584 of=/tmp/big count=1
rm -rf /tmp/sum_double.so /tmp/add_one.so rm -rf /tmp/sum_double.so /tmp/add_one.so
touch /tmp/normal
gcc -g -O0 -fPIC -shared sh/sum_double.c -o /tmp/sum_double.so gcc -g -O0 -fPIC -shared sh/sum_double.c -o /tmp/sum_double.so
gcc -g -O0 -fPIC -shared sh/add_one.c -o /tmp/add_one.so gcc -g -O0 -fPIC -shared sh/add_one.c -o /tmp/add_one.so
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册