From f3043badc58189c2641ccb53311078ae372ca765 Mon Sep 17 00:00:00 2001 From: Leo Yuriev Date: Thu, 19 Nov 2015 14:01:31 +0300 Subject: [PATCH] mdbx: tools/tests - fix minor memleaks (for CI with Valgrind). Change-Id: I0e2217d568259f26cd7c4da800a13577962742f7 --- mdb_chk.c | 32 ++++++++++++++++++++++++-------- mtest6.c | 1 + 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/mdb_chk.c b/mdb_chk.c index b0a0a214..94eab10d 100644 --- a/mdb_chk.c +++ b/mdb_chk.c @@ -122,27 +122,42 @@ error(const char* msg, ...) { } } +static void pagemap_cleanup(void) { + int i; + + for( i = 1; i < MAX_DBI; ++i ) { + if (walk.dbi_names[i]) { + free((void *) walk.dbi_names[i]); + walk.dbi_names[i] = NULL; + } + } + + free(walk.pagemap); + walk.pagemap = NULL; +} + static int pagemap_lookup_dbi(const char* dbi) { static int last; + int i; if (last > 0 && strcmp(walk.dbi_names[last], dbi) == 0) return last; - for(last = 1; walk.dbi_names[last] && last < MAX_DBI; ++last) - if (strcmp(walk.dbi_names[last], dbi) == 0) - return last; + for(i = 1; walk.dbi_names[i] && last < MAX_DBI; ++i) + if (strcmp(walk.dbi_names[i], dbi) == 0) + return last = i; - if (last == MAX_DBI) - return last = -1; + if (i == MAX_DBI) + return -1; - walk.dbi_names[last] = strdup(dbi); + walk.dbi_names[i] = strdup(dbi); if (verbose > 1) { print(" - found '%s' area\n", dbi); fflush(NULL); } - return last; + return last = i; } static void problem_add(const char* object, size_t entry_number, const char* msg, const char *extra, ...) { @@ -581,6 +596,8 @@ int main(int argc, char *argv[]) struct timespec timestamp_start, timestamp_finish; double elapsed; + atexit(pagemap_cleanup); + if (clock_gettime(CLOCK_MONOTONIC, ×tamp_start)) { rc = errno; error("clock_gettime failed, error %d %s\n", rc, mdbx_strerror(rc)); @@ -897,7 +914,6 @@ bailout: mdbx_txn_abort(locktxn); if (env) mdbx_env_close(env); - free(walk.pagemap); fflush(NULL); if (rc) { if (rc < 0) diff --git a/mtest6.c b/mtest6.c index ccd745ed..752c4c5e 100644 --- a/mtest6.c +++ b/mtest6.c @@ -159,6 +159,7 @@ int main(int argc,char * argv[]) mdb_dbi_close(env, dbi); #endif mdb_env_close(env); + free(sval); return 0; }