From 6a0e89c487ed4e3bb715c5b639d84671caca4f21 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Tue, 10 May 2022 09:06:20 +0000 Subject: [PATCH] fix: memory leak --- source/libs/tdb/src/db/tdbBtree.c | 4 +++- source/libs/tdb/src/db/tdbDb.c | 5 ++++- source/libs/tdb/src/db/tdbEnv.c | 16 ++++++++++++++-- source/libs/tdb/src/db/tdbPCache.c | 7 ++++++- source/libs/tdb/src/db/tdbPager.c | 8 +++++++- 5 files changed, 34 insertions(+), 6 deletions(-) diff --git a/source/libs/tdb/src/db/tdbBtree.c b/source/libs/tdb/src/db/tdbBtree.c index 43822cd311..cf7dd50103 100644 --- a/source/libs/tdb/src/db/tdbBtree.c +++ b/source/libs/tdb/src/db/tdbBtree.c @@ -113,7 +113,9 @@ int tdbBtreeOpen(int keyLen, int valLen, SPager *pPager, tdb_cmpr_fn_t kcmpr, SB } int tdbBtreeClose(SBTree *pBt) { - // TODO + if (pBt) { + tdbOsFree(pBt); + } return 0; } diff --git a/source/libs/tdb/src/db/tdbDb.c b/source/libs/tdb/src/db/tdbDb.c index ceaac6dff1..383807cc35 100644 --- a/source/libs/tdb/src/db/tdbDb.c +++ b/source/libs/tdb/src/db/tdbDb.c @@ -66,7 +66,10 @@ int tdbDbOpen(const char *fname, int keyLen, int valLen, tdb_cmpr_fn_t keyCmprFn } int tdbDbClose(TDB *pDb) { - // TODO + if (pDb) { + tdbBtreeClose(pDb->pBt); + tdbOsFree(pDb); + } return 0; } diff --git a/source/libs/tdb/src/db/tdbEnv.c b/source/libs/tdb/src/db/tdbEnv.c index d64b31234e..c0c1343a4f 100644 --- a/source/libs/tdb/src/db/tdbEnv.c +++ b/source/libs/tdb/src/db/tdbEnv.c @@ -56,7 +56,7 @@ int tdbEnvOpen(const char *rootDir, int szPage, int pages, TENV **ppEnv) { pEnv->nPgrHash = 8; tsize = sizeof(SPager *) * pEnv->nPgrHash; - pEnv->pgrHash = tdbRealloc(pEnv->pgrHash, tsize); + pEnv->pgrHash = tdbOsMalloc(tsize); if (pEnv->pgrHash == NULL) { return -1; } @@ -69,7 +69,19 @@ int tdbEnvOpen(const char *rootDir, int szPage, int pages, TENV **ppEnv) { } int tdbEnvClose(TENV *pEnv) { - // TODO + SPager *pPager; + + if (pEnv) { + for (pPager = pEnv->pgrList; pPager; pPager = pEnv->pgrList) { + pEnv->pgrList = pPager->pNext; + tdbPagerClose(pPager); + } + + tdbPCacheClose(pEnv->pCache); + tdbOsFree(pEnv->pgrHash); + tdbOsFree(pEnv); + } + return 0; } diff --git a/source/libs/tdb/src/db/tdbPCache.c b/source/libs/tdb/src/db/tdbPCache.c index 646b83298e..aa05687426 100644 --- a/source/libs/tdb/src/db/tdbPCache.c +++ b/source/libs/tdb/src/db/tdbPCache.c @@ -292,6 +292,10 @@ static int tdbPCacheOpenImpl(SPCache *pCache) { pPage->pFreeNext = pCache->pFree; pCache->pFree = pPage; pCache->nFree++; + + // add to local list + pPage->pCacheNext = pCache->pList; + pCache->pList = pPage; } // Open the hash table @@ -317,9 +321,10 @@ static int tdbPCacheCloseImpl(SPCache *pCache) { for (pPage = pCache->pList; pPage; pPage = pCache->pList) { pCache->pList = pPage->pCacheNext; - tdbPageDestroy(pPage, NULL, NULL); + tdbPageDestroy(pPage, tdbDefaultFree, NULL); } + tdbOsFree(pCache->pgHash); tdbPCacheDestroyLock(pCache); return 0; } diff --git a/source/libs/tdb/src/db/tdbPager.c b/source/libs/tdb/src/db/tdbPager.c index 1941592602..fbd5cb3aac 100644 --- a/source/libs/tdb/src/db/tdbPager.c +++ b/source/libs/tdb/src/db/tdbPager.c @@ -88,7 +88,13 @@ int tdbPagerOpen(SPCache *pCache, const char *fileName, SPager **ppPager) { } int tdbPagerClose(SPager *pPager) { - // TODO + if (pPager) { + if (pPager->inTran) { + tdbOsClose(pPager->jfd); + } + tdbOsClose(pPager->fd); + tdbOsFree(pPager); + } return 0; } -- GitLab