Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf9f98e3

Browse files
committed
From: Igor <igor@sba.miami.edu>
Subject: [PATCHES] pg_dump memory leak patchThis patch fixes a HUGE memory leak problem in pg_dump.Pretty much anything that was allocated was never freed and Purifyreported about 40% possible memory leak and 6% actual leak. I addedfunctions to clear out all the allocated structures. After the patchPurify returns 0 for number of bytes leaked...
1 parenta27fafe commitf9f98e3

File tree

3 files changed

+171
-24
lines changed

3 files changed

+171
-24
lines changed

‎src/bin/pg_dump/common.c

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/bin/pg_dump/common.c,v 1.11 1997/04/12 09:23:59 scrappy Exp $
10+
* $Header: /cvsroot/pgsql/src/bin/pg_dump/common.c,v 1.12 1997/06/02 02:51:49 scrappy Exp $
1111
*
1212
* Modifications - 6/12/96 - dave@bensoft.com - version 1.13.dhb.2
1313
*
@@ -203,12 +203,12 @@ dumpSchema(FILE *fout,
203203
intnumInherits;
204204
intnumAggregates;
205205
intnumOperators;
206-
TypeInfo*tinfo;
207-
FuncInfo*finfo;
208-
AggInfo*agginfo;
209-
TableInfo*tblinfo;
210-
InhInfo*inhinfo;
211-
OprInfo*oprinfo;
206+
TypeInfo*tinfo=NULL;
207+
FuncInfo*finfo=NULL;
208+
AggInfo*agginfo=NULL;
209+
TableInfo*tblinfo=NULL;
210+
InhInfo*inhinfo=NULL;
211+
OprInfo*oprinfo=NULL;
212212

213213
if (g_verbose)fprintf(stderr,"%s reading user-defined types %s\n",
214214
g_comment_start,g_comment_end);
@@ -274,10 +274,14 @@ if (!tablename && fout) {
274274
}
275275

276276
*numTablesPtr=numTables;
277+
clearAggInfo(agginfo,numAggregates);
278+
clearOprInfo(oprinfo,numOperators);
279+
clearTypeInfo(tinfo,numTypes);
280+
clearFuncInfo(finfo,numFuncs);
281+
clearInhInfo(inhinfo,numInherits);
277282
returntblinfo;
278283
}
279284

280-
281285
/*
282286
* dumpSchemaIdx:
283287
* dump indexes at the end for performance
@@ -300,6 +304,7 @@ dumpSchemaIdx(FILE *fout, int *numTablesPtr, const char *tablename,
300304
g_comment_start,g_comment_end);
301305
dumpIndices(fout,indinfo,numIndices,tblinfo,numTables,tablename);
302306
}
307+
clearIndInfo(indinfo,numIndices);
303308
}
304309

305310
/* flagInhAttrs -
@@ -409,9 +414,3 @@ isArchiveName(const char* relname)
409414
{
410415
return (strlen(relname)>1&&relname[1]==',');
411416
}
412-
413-
414-
415-
416-
417-

‎src/bin/pg_dump/pg_dump.c

Lines changed: 141 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
*
2222
*
2323
* IDENTIFICATION
24-
* $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.28 1997/05/06 05:20:18 vadim Exp $
24+
* $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.29 1997/06/02 02:51:53 scrappy Exp $
2525
*
2626
* Modifications - 6/10/96 - dave@bensoft.com - version 1.13.dhb
2727
*
@@ -40,6 +40,10 @@
4040
*
4141
* - Fixed ouput lengths for char and varchar type where the length is variable (-1)
4242
*
43+
* Modifications - 6/1/97 - igor@sba.miami.edu
44+
* - Added functions to free allocated memory used for retrieving
45+
* indices,tables,inheritance,types,functions and aggregates.
46+
* No more leaks reported by Purify.
4347
*-------------------------------------------------------------------------
4448
*/
4549

@@ -230,7 +234,6 @@ dumpClasses_nodumpData(FILE *fout, const char *classname, const bool oids) {
230234
}
231235
fprintf(fout,"\\.\n");
232236
}
233-
PQclear(res);
234237
ret=PQendcopy(res->conn);
235238
if (ret!=0) {
236239
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) {
239242
"Explanation from backend: '%s'.\n"
240243
"The query was: '%s'.\n",
241244
classname,PQerrorMessage(g_conn),query);
245+
if(res)PQclear(res);
242246
exit_nicely(g_conn);
243247
}
248+
if(res)PQclear(res);
244249
}
245250
}
246251

@@ -505,12 +510,11 @@ main(int argc, char** argv)
505510

506511
fflush(g_fout);
507512
fclose(g_fout);
508-
513+
clearTableInfo(tblinfo,numTables);
509514
PQfinish(g_conn);
510515
exit(0);
511516
}
512517

513-
514518
/*
515519
* getTypes:
516520
* read all base types in the system catalogs and return them in the
@@ -722,6 +726,137 @@ getOperators(int *numOprs)
722726
returnoprinfo;
723727
}
724728

729+
void
730+
clearTypeInfo(TypeInfo*tp,intnumTypes)
731+
{
732+
inti;
733+
for(i=0;i<numTypes;++i) {
734+
if(tp[i].oid)free(tp[i].oid);
735+
if(tp[i].typowner)free(tp[i].typowner);
736+
if(tp[i].typname)free(tp[i].typname);
737+
if(tp[i].typlen)free(tp[i].typlen);
738+
if(tp[i].typprtlen)free(tp[i].typprtlen);
739+
if(tp[i].typinput)free(tp[i].typinput);
740+
if(tp[i].typoutput)free(tp[i].typoutput);
741+
if(tp[i].typreceive)free(tp[i].typreceive);
742+
if(tp[i].typsend)free(tp[i].typsend);
743+
if(tp[i].typelem)free(tp[i].typelem);
744+
if(tp[i].typdelim)free(tp[i].typdelim);
745+
if(tp[i].typdefault)free(tp[i].typdefault);
746+
if(tp[i].typrelid)free(tp[i].typrelid);
747+
}
748+
free(tp);
749+
}
750+
751+
void
752+
clearFuncInfo (FuncInfo*fun,intnumFuncs)
753+
{
754+
inti,a;
755+
if(!fun)return;
756+
for(i=0;i<numFuncs;++i) {
757+
if(fun[i].oid)free(fun[i].oid);
758+
if(fun[i].proname)free(fun[i].proname);
759+
if(fun[i].proowner)free(fun[i].proowner);
760+
for(a=0;a<8;++a)
761+
if(fun[i].argtypes[a])free(fun[i].argtypes[a]);
762+
if(fun[i].prorettype)free(fun[i].prorettype);
763+
if(fun[i].prosrc)free(fun[i].prosrc);
764+
if(fun[i].probin)free(fun[i].probin);
765+
}
766+
free(fun);
767+
}
768+
769+
void
770+
clearTableInfo(TableInfo*tblinfo,intnumTables)
771+
{
772+
inti,j;
773+
for(i=0;i<numTables;++i) {
774+
if(tblinfo[i].oid)free (tblinfo[i].oid);
775+
if(tblinfo[i].relname)free (tblinfo[i].relname);
776+
if(tblinfo[i].relarch)free (tblinfo[i].relarch);
777+
if(tblinfo[i].relacl)free (tblinfo[i].relacl);
778+
for (j=0;j<tblinfo[i].numatts;j++) {
779+
if(tblinfo[i].attnames[j])free (tblinfo[i].attnames[j]);
780+
if(tblinfo[i].typnames[j])free (tblinfo[i].typnames[j]);
781+
}
782+
if(tblinfo[i].attlen)free((int*)tblinfo[i].attlen);
783+
if(tblinfo[i].inhAttrs)free((int*)tblinfo[i].inhAttrs);
784+
if(tblinfo[i].attnames)free (tblinfo[i].attnames);
785+
if(tblinfo[i].typnames)free (tblinfo[i].typnames);
786+
}
787+
free(tblinfo);
788+
}
789+
790+
void
791+
clearInhInfo (InhInfo*inh,intnumInherits) {
792+
inti;
793+
if(!inh)return;
794+
for(i=0;i<numInherits;++i) {
795+
if(inh[i].oid)free(inh[i].oid);
796+
if(inh[i].inhrel)free(inh[i].inhrel);
797+
if(inh[i].inhparent)free(inh[i].inhparent);}
798+
free(inh);
799+
}
800+
801+
void
802+
clearOprInfo(OprInfo*opr,intnumOprs){
803+
inti;
804+
if(!opr)return;
805+
for(i=0;i<numOprs;++i) {
806+
if(opr[i].oid)free(opr[i].oid);
807+
if(opr[i].oprname)free(opr[i].oprname);
808+
if(opr[i].oprkind)free(opr[i].oprkind);
809+
if(opr[i].oprcode)free(opr[i].oprcode);
810+
if(opr[i].oprleft)free(opr[i].oprleft);
811+
if(opr[i].oprright)free(opr[i].oprright);
812+
if(opr[i].oprcom)free(opr[i].oprcom);
813+
if(opr[i].oprnegate)free(opr[i].oprnegate);
814+
if(opr[i].oprrest)free(opr[i].oprrest);
815+
if(opr[i].oprjoin)free(opr[i].oprjoin);
816+
if(opr[i].oprcanhash)free(opr[i].oprcanhash);
817+
if(opr[i].oprlsortop)free(opr[i].oprlsortop);
818+
if(opr[i].oprrsortop)free(opr[i].oprrsortop);
819+
}
820+
free(opr);
821+
}
822+
823+
void
824+
clearIndInfo(IndInfo*ind,intnumIndices)
825+
{
826+
inti,a;
827+
if(!ind)return;
828+
for(i=0;i<numIndices;++i) {
829+
if(ind[i].indexrelname)free(ind[i].indexrelname);
830+
if(ind[i].indrelname)free(ind[i].indrelname);
831+
if(ind[i].indamname)free(ind[i].indamname);
832+
if(ind[i].indproc)free(ind[i].indproc);
833+
if(ind[i].indisunique)free(ind[i].indisunique);
834+
for(a=0;a<INDEX_MAX_KEYS;++a) {
835+
if(ind[i].indkey[a])free(ind[i].indkey[a]);
836+
if(ind[i].indclass[a])free(ind[i].indclass[a]);}
837+
}
838+
free(ind);
839+
}
840+
841+
void
842+
clearAggInfo(AggInfo*agginfo,intnumArgs)
843+
{
844+
inti;
845+
if(!agginfo)return;
846+
for(i=0;i<numArgs;++i) {
847+
if(agginfo[i].oid)free (agginfo[i].oid);
848+
if(agginfo[i].aggname)free (agginfo[i].aggname);
849+
if(agginfo[i].aggtransfn1)free (agginfo[i].aggtransfn1);
850+
if(agginfo[i].aggtransfn2)free (agginfo[i].aggtransfn2);
851+
if(agginfo[i].aggfinalfn)free (agginfo[i].aggfinalfn);
852+
if(agginfo[i].aggtranstype1)free (agginfo[i].aggtranstype1);
853+
if(agginfo[i].aggbasetype)free (agginfo[i].aggbasetype);
854+
if(agginfo[i].aggtranstype2)free (agginfo[i].aggtranstype2);
855+
if(agginfo[i].agginitval1)free (agginfo[i].agginitval1);
856+
if(agginfo[i].agginitval2)free (agginfo[i].agginitval2);
857+
}
858+
free (agginfo);
859+
}
725860

726861
/*
727862
* getAggregates:
@@ -778,7 +913,7 @@ getAggregates(int *numAggs)
778913
*numAggs=ntups;
779914

780915
agginfo= (AggInfo*)malloc(ntups*sizeof(AggInfo));
781-
916+
782917
i_oid=PQfnumber(res,"oid");
783918
i_aggname=PQfnumber(res,"aggname");
784919
i_aggtransfn1=PQfnumber(res,"aggtransfn1");
@@ -1207,7 +1342,7 @@ getIndices(int *numIndices)
12071342
}
12081343
PQclear(res);
12091344
res=PQexec(g_conn,"end");
1210-
1345+
if(res)PQclear(res);
12111346
returnindinfo;
12121347
}
12131348

‎src/bin/pg_dump/pg_dump.h

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@
55
*
66
* Copyright (c) 1994, Regents of the University of California
77
*
8-
* $Id: pg_dump.h,v 1.14 1997/05/07 02:59:59 scrappy Exp $
8+
* $Id: pg_dump.h,v 1.15 1997/06/02 02:52:06 scrappy Exp $
99
*
1010
* Modifications - 6/12/96 - dave@bensoft.com - version 1.13.dhb.2
1111
*
1212
* - Fixed dumpTable output to output lengths for char and varchar types!
1313
* - Added single. quote to twin single quote expansion for 'insert' string
1414
* mode.
15+
*
16+
* Modifications - 6/1/97 - igor@sba.miami.edu
17+
* - Added extern's for the functions that clear allocated memory
18+
* in pg_dump.c
1519
*-------------------------------------------------------------------------
1620
*/
1721

@@ -86,9 +90,9 @@ typedef struct _inhInfo {
8690
typedefstruct_indInfo {
8791
char*indexrelname;/* name of the secondary index class */
8892
char*indrelname;/* name of the indexed heap class */
89-
char*indamname;/* name of the access method (e.g. btree, rtree, etc.) */
90-
char*indproc;/* oid of the function to compute the index, 0 if none*/
91-
char*indkey[INDEX_MAX_KEYS];/* attribute numbers of the key attributes */
93+
char*indamname;/* name of the access method (e.g. btree, rtree, etc.) */
94+
char*indproc;/* oid of the function to compute the index, 0 if none*/
95+
char*indkey[INDEX_MAX_KEYS];/* attribute numbers of the key attributes */
9296
char*indclass[INDEX_MAX_KEYS];/* opclass of the keys */
9397
char*indisunique;/* is this index unique? */
9498
}IndInfo;
@@ -179,6 +183,15 @@ extern bool isViewRule(char *relname);
179183
externTypeInfo*getTypes(int*numTypes);
180184
externFuncInfo*getFuncs(int*numFuncs);
181185
externAggInfo*getAggregates(int*numAggregates);
186+
187+
externvoidclearAggInfo(AggInfo*,int);
188+
externvoidclearFuncInfo(FuncInfo*,int);
189+
externvoidclearInhInfo(InhInfo*,int);
190+
externvoidclearIndInfo(IndInfo*,int);
191+
externvoidclearOprInfo(OprInfo*,int);
192+
externvoidclearTypeInfo(TypeInfo*,int);
193+
externvoidclearTableInfo(TableInfo*,int);
194+
182195
externOprInfo*getOperators(int*numOperators);
183196
externTableInfo*getTables(int*numTables);
184197
externInhInfo*getInherits(int*numInherits);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp