From f9f98e3aff781271b5f3df4f8df61acc83dc6ce3 Mon Sep 17 00:00:00 2001 From: "Marc G. Fournier" Date: Mon, 2 Jun 1997 02:52:06 +0000 Subject: [PATCH] From: Igor Subject: [PATCHES] pg_dump memory leak patch This patch fixes a HUGE memory leak problem in pg_dump. Pretty much anything that was allocated was never freed and Purify reported about 40% possible memory leak and 6% actual leak. I added functions to clear out all the allocated structures. After the patch Purify returns 0 for number of bytes leaked... --- src/bin/pg_dump/common.c | 27 ++++--- src/bin/pg_dump/pg_dump.c | 147 ++++++++++++++++++++++++++++++++++++-- src/bin/pg_dump/pg_dump.h | 21 ++++-- 3 files changed, 171 insertions(+), 24 deletions(-) diff --git a/src/bin/pg_dump/common.c b/src/bin/pg_dump/common.c index f12510dc3b..1fdd848672 100644 --- a/src/bin/pg_dump/common.c +++ b/src/bin/pg_dump/common.c @@ -7,7 +7,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/bin/pg_dump/common.c,v 1.11 1997/04/12 09:23:59 scrappy Exp $ + * $Header: /cvsroot/pgsql/src/bin/pg_dump/common.c,v 1.12 1997/06/02 02:51:49 scrappy Exp $ * * Modifications - 6/12/96 - dave@bensoft.com - version 1.13.dhb.2 * @@ -203,12 +203,12 @@ dumpSchema(FILE *fout, int numInherits; int numAggregates; int numOperators; - TypeInfo *tinfo; - FuncInfo *finfo; - AggInfo *agginfo; - TableInfo *tblinfo; - InhInfo *inhinfo; - OprInfo *oprinfo; + TypeInfo *tinfo=NULL; + FuncInfo *finfo=NULL; + AggInfo *agginfo=NULL; + TableInfo *tblinfo=NULL; + InhInfo *inhinfo=NULL; + OprInfo *oprinfo=NULL; if (g_verbose) fprintf(stderr,"%s reading user-defined types %s\n", g_comment_start, g_comment_end); @@ -274,10 +274,14 @@ if (!tablename && fout) { } *numTablesPtr = numTables; + clearAggInfo(agginfo,numAggregates); + clearOprInfo(oprinfo,numOperators); + clearTypeInfo(tinfo, numTypes); + clearFuncInfo(finfo,numFuncs); + clearInhInfo(inhinfo,numInherits); return tblinfo; } - /* * dumpSchemaIdx: * dump indexes at the end for performance @@ -300,6 +304,7 @@ dumpSchemaIdx(FILE *fout, int *numTablesPtr, const char *tablename, g_comment_start, g_comment_end); dumpIndices(fout, indinfo, numIndices, tblinfo, numTables, tablename); } + clearIndInfo(indinfo,numIndices); } /* flagInhAttrs - @@ -409,9 +414,3 @@ isArchiveName(const char* relname) { return (strlen(relname) > 1 && relname[1] == ','); } - - - - - - diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c index 17e21a678a..1c481b78ac 100644 --- a/src/bin/pg_dump/pg_dump.c +++ b/src/bin/pg_dump/pg_dump.c @@ -21,7 +21,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.28 1997/05/06 05:20:18 vadim Exp $ + * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.29 1997/06/02 02:51:53 scrappy Exp $ * * Modifications - 6/10/96 - dave@bensoft.com - version 1.13.dhb * @@ -40,6 +40,10 @@ * * - Fixed ouput lengths for char and varchar type where the length is variable (-1) * + * Modifications - 6/1/97 - igor@sba.miami.edu + * - Added functions to free allocated memory used for retrieving + * indices,tables,inheritance,types,functions and aggregates. + * No more leaks reported by Purify. *------------------------------------------------------------------------- */ @@ -230,7 +234,6 @@ dumpClasses_nodumpData(FILE *fout, const char *classname, const bool oids) { } fprintf(fout, "\\.\n"); } - PQclear(res); ret = PQendcopy(res->conn); if (ret != 0) { fprintf(stderr, "SQL query to dump the contents of Table %s " @@ -239,8 +242,10 @@ dumpClasses_nodumpData(FILE *fout, const char *classname, const bool oids) { "Explanation from backend: '%s'.\n" "The query was: '%s'.\n", classname, PQerrorMessage(g_conn), query); + if(res) PQclear(res); exit_nicely(g_conn); } + if(res) PQclear(res); } } @@ -505,12 +510,11 @@ main(int argc, char** argv) fflush(g_fout); fclose(g_fout); - + clearTableInfo(tblinfo, numTables); PQfinish(g_conn); exit(0); } - /* * getTypes: * read all base types in the system catalogs and return them in the @@ -722,6 +726,137 @@ getOperators(int *numOprs) return oprinfo; } +void +clearTypeInfo(TypeInfo *tp, int numTypes) +{ +int i; +for(i=0;i