fix: memory leak of geos

This commit is contained in:
kailixu 2024-08-26 09:35:44 +08:00
parent 095fd4efc3
commit d9883ac057
1 changed files with 35 additions and 32 deletions

View File

@ -18,46 +18,50 @@
#include "tutil.h" #include "tutil.h"
static threadlocal TdThreadKey tlGeosCtxKey = 0; static threadlocal TdThreadKey tlGeosCtxKey = 0;
static threadlocal SGeosContext tlGeosCtxObj = {0}; static threadlocal SGeosContext *tlGeosCtx = NULL;
static void destroyThreadLocalGeosCtx(void *param) { static void destroyThreadLocalGeosCtx(void *param) {
SGeosContext *tlGeosCtx = &tlGeosCtxObj; SGeosContext *pGeosCtx = (SGeosContext *)param;
if (tlGeosCtx->WKTReader) { if (!pGeosCtx) {
GEOSWKTReader_destroy_r(tlGeosCtx->handle, tlGeosCtx->WKTReader); return;
tlGeosCtx->WKTReader = NULL; }
if (pGeosCtx->WKTReader) {
GEOSWKTReader_destroy_r(pGeosCtx->handle, pGeosCtx->WKTReader);
pGeosCtx->WKTReader = NULL;
} }
if (tlGeosCtx->WKTWriter) { if (pGeosCtx->WKTWriter) {
GEOSWKTWriter_destroy_r(tlGeosCtx->handle, tlGeosCtx->WKTWriter); GEOSWKTWriter_destroy_r(pGeosCtx->handle, pGeosCtx->WKTWriter);
tlGeosCtx->WKTWriter = NULL; pGeosCtx->WKTWriter = NULL;
} }
if (tlGeosCtx->WKBReader) { if (pGeosCtx->WKBReader) {
GEOSWKBReader_destroy_r(tlGeosCtx->handle, tlGeosCtx->WKBReader); GEOSWKBReader_destroy_r(pGeosCtx->handle, pGeosCtx->WKBReader);
tlGeosCtx->WKBReader = NULL; pGeosCtx->WKBReader = NULL;
} }
if (tlGeosCtx->WKBWriter) { if (pGeosCtx->WKBWriter) {
GEOSWKBWriter_destroy_r(tlGeosCtx->handle, tlGeosCtx->WKBWriter); GEOSWKBWriter_destroy_r(pGeosCtx->handle, pGeosCtx->WKBWriter);
tlGeosCtx->WKBWriter = NULL; pGeosCtx->WKBWriter = NULL;
} }
if (tlGeosCtx->WKTRegex) { if (pGeosCtx->WKTRegex) {
destroyRegexes(tlGeosCtx->WKTRegex, tlGeosCtx->WKTMatchData); destroyRegexes(pGeosCtx->WKTRegex, pGeosCtx->WKTMatchData);
tlGeosCtx->WKTRegex = NULL; pGeosCtx->WKTRegex = NULL;
tlGeosCtx->WKTMatchData = NULL; pGeosCtx->WKTMatchData = NULL;
} }
if (tlGeosCtx->handle) { if (pGeosCtx->handle) {
GEOS_finish_r(tlGeosCtx->handle); GEOS_finish_r(pGeosCtx->handle);
tlGeosCtx->handle = NULL; pGeosCtx->handle = NULL;
} }
taosMemoryFree(pGeosCtx);
} }
SGeosContext *acquireThreadLocalGeosCtx() { return taosThreadGetSpecific(tlGeosCtxKey); } SGeosContext *acquireThreadLocalGeosCtx() { return tlGeosCtx; }
int32_t getThreadLocalGeosCtx(SGeosContext **ppCtx) { int32_t getThreadLocalGeosCtx(SGeosContext **ppCtx) {
if ((*ppCtx = taosThreadGetSpecific(tlGeosCtxKey))) { if ((*ppCtx = tlGeosCtx)) {
return 0; return 0;
} }
@ -66,17 +70,17 @@ int32_t getThreadLocalGeosCtx(SGeosContext **ppCtx) {
TAOS_CHECK_EXIT(TAOS_SYSTEM_ERROR(errno)); TAOS_CHECK_EXIT(TAOS_SYSTEM_ERROR(errno));
} }
if ((taosThreadSetSpecific(tlGeosCtxKey, &tlGeosCtxObj)) != 0) { SGeosContext *tlGeosCtxObj = (SGeosContext *)taosMemoryCalloc(1, sizeof(SGeosContext));
if (!tlGeosCtxObj) {
TAOS_CHECK_EXIT(TSDB_CODE_OUT_OF_MEMORY);
}
if ((taosThreadSetSpecific(tlGeosCtxKey, (const void *)tlGeosCtxObj)) != 0) {
taosMemoryFreeClear(tlGeosCtxObj);
TAOS_CHECK_EXIT(TAOS_SYSTEM_ERROR(errno)); TAOS_CHECK_EXIT(TAOS_SYSTEM_ERROR(errno));
} }
if (!(*ppCtx = taosThreadGetSpecific(tlGeosCtxKey))) { *ppCtx = tlGeosCtx = tlGeosCtxObj;
if (errno != 0) {
TAOS_CHECK_EXIT(TAOS_SYSTEM_ERROR(errno));
} else {
TAOS_CHECK_EXIT(TSDB_CODE_NOT_FOUND);
}
}
_exit: _exit:
if (code != 0) { if (code != 0) {
*ppCtx = NULL; *ppCtx = NULL;
@ -86,6 +90,5 @@ _exit:
} }
const char *getGeosErrMsg(int32_t code) { const char *getGeosErrMsg(int32_t code) {
SGeosContext *tlGeosCtx = taosThreadGetSpecific(tlGeosCtxKey);
return (tlGeosCtx && tlGeosCtx->errMsg[0] != 0) ? tlGeosCtx->errMsg : (code ? tstrerror(code) : ""); return (tlGeosCtx && tlGeosCtx->errMsg[0] != 0) ? tlGeosCtx->errMsg : (code ? tstrerror(code) : "");
} }