fix: memory leak of geos

This commit is contained in:
kailixu 2024-08-20 09:03:57 +08:00
parent 0531a4f4bd
commit 6055b9172e
2 changed files with 15 additions and 40 deletions

View File

@ -37,10 +37,7 @@ int32_t initCtxMakePoint() {
int32_t code = TSDB_CODE_FAILED; int32_t code = TSDB_CODE_FAILED;
SGeosContext *geosCtx = getThreadLocalGeosCtx(); SGeosContext *geosCtx = getThreadLocalGeosCtx();
if (!geosCtx) { if (!geosCtx) return TSDB_CODE_OUT_OF_MEMORY;
code = TSDB_CODE_OUT_OF_MEMORY;
return code;
}
if (geosCtx->handle == NULL) { if (geosCtx->handle == NULL) {
geosCtx->handle = GEOS_init_r(); geosCtx->handle = GEOS_init_r();
@ -67,10 +64,7 @@ int32_t doMakePoint(double x, double y, unsigned char **outputGeom, size_t *size
int32_t code = TSDB_CODE_FAILED; int32_t code = TSDB_CODE_FAILED;
SGeosContext *geosCtx = getThreadLocalGeosCtx(); SGeosContext *geosCtx = getThreadLocalGeosCtx();
if (!geosCtx) { if (!geosCtx) return TSDB_CODE_OUT_OF_MEMORY;
code = TSDB_CODE_OUT_OF_MEMORY;
return code;
}
GEOSGeometry *geom = NULL; GEOSGeometry *geom = NULL;
unsigned char *wkb = NULL; unsigned char *wkb = NULL;
@ -177,10 +171,7 @@ int32_t initCtxGeomFromText() {
int32_t code = TSDB_CODE_FAILED; int32_t code = TSDB_CODE_FAILED;
SGeosContext *geosCtx = getThreadLocalGeosCtx(); SGeosContext *geosCtx = getThreadLocalGeosCtx();
if (!geosCtx) { if (!geosCtx) return TSDB_CODE_OUT_OF_MEMORY;
code = TSDB_CODE_OUT_OF_MEMORY;
return code;
}
if (geosCtx->handle == NULL) { if (geosCtx->handle == NULL) {
geosCtx->handle = GEOS_init_r(); geosCtx->handle = GEOS_init_r();
@ -218,10 +209,7 @@ int32_t doGeomFromText(const char *inputWKT, unsigned char **outputGeom, size_t
int32_t code = TSDB_CODE_FAILED; int32_t code = TSDB_CODE_FAILED;
SGeosContext *geosCtx = getThreadLocalGeosCtx(); SGeosContext *geosCtx = getThreadLocalGeosCtx();
if (!geosCtx) { if (!geosCtx) return TSDB_CODE_OUT_OF_MEMORY;
code = TSDB_CODE_OUT_OF_MEMORY;
return code;
}
GEOSGeometry *geom = NULL; GEOSGeometry *geom = NULL;
unsigned char *wkb = NULL; unsigned char *wkb = NULL;
@ -258,10 +246,7 @@ int32_t initCtxAsText() {
int32_t code = TSDB_CODE_FAILED; int32_t code = TSDB_CODE_FAILED;
SGeosContext *geosCtx = getThreadLocalGeosCtx(); SGeosContext *geosCtx = getThreadLocalGeosCtx();
if (!geosCtx) { if (!geosCtx) return TSDB_CODE_OUT_OF_MEMORY;
code = TSDB_CODE_OUT_OF_MEMORY;
return code;
}
if (geosCtx->handle == NULL) { if (geosCtx->handle == NULL) {
geosCtx->handle = GEOS_init_r(); geosCtx->handle = GEOS_init_r();
@ -299,10 +284,7 @@ int32_t doAsText(const unsigned char *inputGeom, size_t size, char **outputWKT)
int32_t code = TSDB_CODE_FAILED; int32_t code = TSDB_CODE_FAILED;
SGeosContext *geosCtx = getThreadLocalGeosCtx(); SGeosContext *geosCtx = getThreadLocalGeosCtx();
if (!geosCtx) { if (!geosCtx) return TSDB_CODE_OUT_OF_MEMORY;
code = TSDB_CODE_OUT_OF_MEMORY;
return code;
}
GEOSGeometry *geom = NULL; GEOSGeometry *geom = NULL;
char *wkt = NULL; char *wkt = NULL;
@ -335,10 +317,7 @@ int32_t initCtxRelationFunc() {
int32_t code = TSDB_CODE_FAILED; int32_t code = TSDB_CODE_FAILED;
SGeosContext *geosCtx = getThreadLocalGeosCtx(); SGeosContext *geosCtx = getThreadLocalGeosCtx();
if (!geosCtx) { if (!geosCtx) return TSDB_CODE_OUT_OF_MEMORY;
code = TSDB_CODE_OUT_OF_MEMORY;
return code;
}
if (geosCtx->handle == NULL) { if (geosCtx->handle == NULL) {
geosCtx->handle = GEOS_init_r(); geosCtx->handle = GEOS_init_r();
@ -365,9 +344,7 @@ int32_t doGeosRelation(const GEOSGeometry *geom1, const GEOSPreparedGeometry *pr
_geosPreparedRelationFunc_t swappedPreparedRelationFn) { _geosPreparedRelationFunc_t swappedPreparedRelationFn) {
SGeosContext *geosCtx = getThreadLocalGeosCtx(); SGeosContext *geosCtx = getThreadLocalGeosCtx();
if (!geosCtx) { if (!geosCtx) return TSDB_CODE_OUT_OF_MEMORY;
return TSDB_CODE_OUT_OF_MEMORY;
}
if (!preparedGeom1) { if (!preparedGeom1) {
if (!swapped) { if (!swapped) {
@ -429,11 +406,6 @@ int32_t doContainsProperly(const GEOSGeometry *geom1, const GEOSPreparedGeometry
// need to call destroyGeometry(outputGeom, outputPreparedGeom) later // need to call destroyGeometry(outputGeom, outputPreparedGeom) later
int32_t readGeometry(const unsigned char *input, GEOSGeometry **outputGeom, int32_t readGeometry(const unsigned char *input, GEOSGeometry **outputGeom,
const GEOSPreparedGeometry **outputPreparedGeom) { const GEOSPreparedGeometry **outputPreparedGeom) {
SGeosContext *geosCtx = getThreadLocalGeosCtx();
if (!geosCtx) {
return TSDB_CODE_OUT_OF_MEMORY;
}
ASSERT(outputGeom); // it is not allowed if outputGeom is NULL ASSERT(outputGeom); // it is not allowed if outputGeom is NULL
*outputGeom = NULL; *outputGeom = NULL;
@ -445,6 +417,10 @@ int32_t readGeometry(const unsigned char *input, GEOSGeometry **outputGeom,
return TSDB_CODE_SUCCESS; return TSDB_CODE_SUCCESS;
} }
SGeosContext *geosCtx = getThreadLocalGeosCtx();
if (!geosCtx) return TSDB_CODE_OUT_OF_MEMORY;
*outputGeom = GEOSWKBReader_read_r(geosCtx->handle, geosCtx->WKBReader, varDataVal(input), varDataLen(input)); *outputGeom = GEOSWKBReader_read_r(geosCtx->handle, geosCtx->WKBReader, varDataVal(input), varDataLen(input));
if (*outputGeom == NULL) { if (*outputGeom == NULL) {
return TSDB_CODE_FUNC_FUNTION_PARA_VALUE; return TSDB_CODE_FUNC_FUNTION_PARA_VALUE;
@ -461,7 +437,8 @@ int32_t readGeometry(const unsigned char *input, GEOSGeometry **outputGeom,
} }
void destroyGeometry(GEOSGeometry **geom, const GEOSPreparedGeometry **preparedGeom) { void destroyGeometry(GEOSGeometry **geom, const GEOSPreparedGeometry **preparedGeom) {
SGeosContext *geosCtx = getThreadLocalGeosCtx(); SGeosContext *geosCtx = acquireThreadLocalGeosCtx();
if (!geosCtx) return;
if (preparedGeom && *preparedGeom) { if (preparedGeom && *preparedGeom) {
GEOSPreparedGeom_destroy_r(geosCtx->handle, *preparedGeom); GEOSPreparedGeom_destroy_r(geosCtx->handle, *preparedGeom);

View File

@ -31,9 +31,7 @@ static threadlocal SGeosContext *tlGeosCtx = NULL;
SGeosContext *acquireThreadLocalGeosCtx() { return tlGeosCtx; } SGeosContext *acquireThreadLocalGeosCtx() { return tlGeosCtx; }
SGeosContext *getThreadLocalGeosCtx() { SGeosContext *getThreadLocalGeosCtx() {
if (tlGeosCtx) { if (tlGeosCtx) return tlGeosCtx;
return tlGeosCtx;
}
taosWLockLatch(&sGeosPool.lock); taosWLockLatch(&sGeosPool.lock);
if (sGeosPool.size >= sGeosPool.capacity) { if (sGeosPool.size >= sGeosPool.capacity) {