From 9c5f7d6656d865499e699907a0967e2b96a0da3f Mon Sep 17 00:00:00 2001 From: factosea <285808407@qq.com> Date: Wed, 17 Apr 2024 20:06:29 +0800 Subject: [PATCH] thread mem leak --- include/util/tcompare.h | 1 + source/util/src/tcompare.c | 22 ++++++++++++---------- source/util/src/tworker.c | 2 ++ 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/include/util/tcompare.h b/include/util/tcompare.h index 65457b287a..9694bee92d 100644 --- a/include/util/tcompare.h +++ b/include/util/tcompare.h @@ -83,6 +83,7 @@ int32_t compareLenBinaryVal(const void *pLeft, const void *pRight); int32_t comparestrRegexMatch(const void *pLeft, const void *pRight); int32_t comparestrRegexNMatch(const void *pLeft, const void *pRight); +void DestoryThreadLocalRegComp(); int32_t comparewcsRegexMatch(const void *pLeft, const void *pRight); int32_t comparewcsRegexNMatch(const void *pLeft, const void *pRight); diff --git a/source/util/src/tcompare.c b/source/util/src/tcompare.c index ea4ee40a29..d845e5aed7 100644 --- a/source/util/src/tcompare.c +++ b/source/util/src/tcompare.c @@ -24,7 +24,6 @@ #include "tutil.h" #include "types.h" #include "osString.h" -#include int32_t setChkInBytes1(const void *pLeft, const void *pRight) { return NULL != taosHashGet((SHashObj *)pRight, pLeft, 1) ? 1 : 0; @@ -1204,19 +1203,16 @@ int32_t comparestrRegexNMatch(const void *pLeft, const void *pRight) { return comparestrRegexMatch(pLeft, pRight) ? 0 : 1; } +static threadlocal regex_t pRegex; +static threadlocal char *pOldPattern; static regex_t *threadGetRegComp(const char *pPattern) { - static __thread regex_t pRegex; - static __thread char *pOldPattern; if (NULL != pOldPattern) { if( strcmp(pOldPattern, pPattern) == 0) { return &pRegex; } else { - regfree(&pRegex); - taosMemoryFree(pOldPattern); - pOldPattern == NULL; + DestoryThreadLocalRegComp(); } } - pOldPattern = taosMemoryMalloc(strlen(pPattern) + 1); if (NULL == pOldPattern) { uError("Failed to Malloc when compile regex pattern %s.", pPattern); @@ -1229,14 +1225,20 @@ static regex_t *threadGetRegComp(const char *pPattern) { char msgbuf[256] = {0}; regerror(ret, &pRegex, msgbuf, tListLen(msgbuf)); uError("Failed to compile regex pattern %s. reason %s", pPattern, msgbuf); - regfree(&pRegex); - taosMemoryFree(pOldPattern); - pOldPattern == NULL; + DestoryThreadLocalRegComp(); return NULL; } return &pRegex; } +void DestoryThreadLocalRegComp() { + if (NULL != pOldPattern) { + regfree(&pRegex); + taosMemoryFree(pOldPattern); + pOldPattern == NULL; + } +} + static int32_t doExecRegexMatch(const char *pString, const char *pPattern) { int32_t ret = 0; char msgbuf[256] = {0}; diff --git a/source/util/src/tworker.c b/source/util/src/tworker.c index 0712010458..d2651c1d2c 100644 --- a/source/util/src/tworker.c +++ b/source/util/src/tworker.c @@ -18,6 +18,7 @@ #include "taoserror.h" #include "tgeosctx.h" #include "tlog.h" +#include "tcompare.h" #define QUEUE_THRESHOLD (1000 * 1000) @@ -103,6 +104,7 @@ static void *tQWorkerThreadFp(SQueueWorker *worker) { } destroyThreadLocalGeosCtx(); + DestoryThreadLocalRegComp(); return NULL; }