From 7695ffc594280532f802d53b9531e1994d340e6b Mon Sep 17 00:00:00 2001 From: Marcelo Matus Date: Mon, 7 Mar 2005 20:39:46 +0000 Subject: [PATCH] fix several memory leaks git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@7036 626c5289-ae23-0410-ae9c-e8d60b6d4f22 --- Source/CParse/templ.c | 11 +++++- Source/Swig/cwrap.c | 26 +++++++++--- Source/Swig/fragment.c | 12 ++++-- Source/Swig/naming.c | 2 +- Source/Swig/parms.c | 89 +++++++++++++++++++++++++----------------- Source/Swig/stype.c | 12 +++++- Source/Swig/typemap.c | 34 +++++++++++----- Source/Swig/typesys.c | 47 ++++++++++++++++------ 8 files changed, 164 insertions(+), 69 deletions(-) diff --git a/Source/CParse/templ.c b/Source/CParse/templ.c index 01e640b6a..9490ba4a2 100644 --- a/Source/CParse/templ.c +++ b/Source/CParse/templ.c @@ -158,7 +158,9 @@ cparse_template_expand(Node *n, String *tname, String *rname, String *templatear } name = Getattr(n,"sym:name"); if (name && Strstr(name,"<")) { - Setattr(n,"sym:name", Copy(tname)); + String *sn = Copy(tname); + Setattr(n,"sym:name", sn); + Delete(sn); } else { Replace(name,tname,rname, DOH_REPLACE_ANY); } @@ -251,12 +253,14 @@ Swig_cparse_template_expand(Node *n, String *rname, ParmList *tparms, Symtab *ts if (ptype && tptype) { partial_type = partial_arg(tptype,ptype); /* Printf(stdout,"partial '%s' '%s' ---> '%s'\n", tptype, ptype, partial_type); */ - Setattr(tp,"type",partial_type); + Setattr(tp,"type",partial_type); + Delete(partial_type); } p = nextSibling(p); tp = nextSibling(tp); } assert(ParmList_len(ptargs) == ParmList_len(tparms)); + Delete(ptargs); } if (0) { @@ -377,6 +381,7 @@ Swig_cparse_template_expand(Node *n, String *rname, ParmList *tparms, Symtab *ts String *qn = Swig_symbol_type_qualify(b.item,tscope); Clear(b.item); Append(b.item,qn); + Delete(qn); } } } @@ -384,6 +389,7 @@ Swig_cparse_template_expand(Node *n, String *rname, ParmList *tparms, Symtab *ts Delete(cpatchlist); Delete(typelist); Delete(tbase); + Delete(templateargs); /* set_nodeType(n,"template");*/ return 0; @@ -427,6 +433,7 @@ template_locate(String *name, Parm *tparms, Symtab *tscope) { if (ty) { SwigType *nt = Swig_symbol_type_qualify(ty,tscope); Setattr(p,"type",nt); + Delete(nt); } p = nextSibling(p); } diff --git a/Source/Swig/cwrap.c b/Source/Swig/cwrap.c index 3aac66b2c..2e7f6f397 100644 --- a/Source/Swig/cwrap.c +++ b/Source/Swig/cwrap.c @@ -59,9 +59,15 @@ Swig_clocal(SwigType *t, const String_or_char *name, const String_or_char *value switch(SwigType_type(t)) { case T_REFERENCE: if (value) { - Printf(decl,"%s = (%s) &%s_defvalue", SwigType_lstr(t,name), SwigType_lstr(t,0), name); + String *lstrname = SwigType_lstr(t,name); + String *lstr = SwigType_lstr(t,0); + Printf(decl,"%s = (%s) &%s_defvalue", lstrname, lstr, name); + Delete(lstrname); + Delete(lstr); } else { - Printf(decl,"%s = 0", SwigType_lstr(t,name)); + String *lstrname = SwigType_lstr(t,name); + Printf(decl,"%s = 0", lstrname); + Delete(lstrname); } break; case T_VOID: @@ -72,9 +78,15 @@ Swig_clocal(SwigType *t, const String_or_char *name, const String_or_char *value default: if (value) { - Printf(decl,"%s = (%s) %s", SwigType_lstr(t,name), SwigType_lstr(t,0), SwigType_lcaststr(t,value)); + String *lcaststr = SwigType_lcaststr(t,value); + String *lstr = SwigType_lstr(t,0); + Printf(decl,"%s = (%s) %s", SwigType_lstr(t,name), lstr, lcaststr); + Delete(lcaststr); + Delete(lstr); } else { - Printf(decl,"%s", SwigType_lstr(t,name)); + String *lstrname = SwigType_lstr(t,name); + Printf(decl,"%s", lstrname); + Delete(lstrname); } } return decl; @@ -185,7 +197,9 @@ int Swig_cargs(Wrapper *w, ParmList *p) { defvalue = NewStringf("%s = %s", SwigType_lstr(tvalue,defname),qvalue); } else { /* user type, we copy the reference value */ - defvalue = NewStringf("%s = %s",SwigType_str(type,defname),qvalue); + String *str = SwigType_str(type,defname); + defvalue = NewStringf("%s = %s",str,qvalue); + Delete(str); } Wrapper_add_localv(w,defname, defvalue, NIL); Delete(tvalue); @@ -827,6 +841,7 @@ Swig_MethodToFunction(Node *n, String *classname, int flags) { Setattr(n,"parms",p); Delete(p); Delete(self); + Delete(parms); return SWIG_OK; } @@ -926,6 +941,7 @@ Swig_ConstructorToFunction(Node *n, String *classname, for (p2 = parms; p2; p2 = nextSibling(p2)) { p3 = CopyParm(p2); set_nextSibling(p, p3); + Delete(p3); p = p3; } } else diff --git a/Source/Swig/fragment.c b/Source/Swig/fragment.c index 2c426f4d9..c23957621 100644 --- a/Source/Swig/fragment.c +++ b/Source/Swig/fragment.c @@ -49,13 +49,19 @@ Swig_fragment_register(Node* fragment) { fragments = NewHash(); } if (!Getattr(fragments,name)) { - String *section = Getattr(fragment,"section"); + String *section = Copy(Getattr(fragment,"section")); String *ccode = Copy(Getattr(fragment,"code")); Hash *kwargs = Getattr(fragment,"kwargs"); - Setmeta(ccode,"section",Copy(section)); - if (kwargs) Setmeta(ccode,"kwargs",Copy(kwargs)); + Setmeta(ccode,"section",section); + if (kwargs) { + kwargs = Copy(kwargs); + Setmeta(ccode,"kwargs",kwargs); + Delete(kwargs); + } Setattr(fragments,name,ccode); if (debug) Printf(stdout,"registering fragment %s %s\n",name,section); + Delete(section); + Delete(ccode); } } } diff --git a/Source/Swig/naming.c b/Source/Swig/naming.c index dd7f9eca4..9ea07164f 100644 --- a/Source/Swig/naming.c +++ b/Source/Swig/naming.c @@ -404,7 +404,7 @@ Swig_name_object_set(Hash *namehash, String *name, SwigType *decl, DOH *object) } /* Add an object based on the declarator value */ if (!decl) { - Setattr(n,NewString("*"),object); + Setattr(n,"*",object); } else { Setattr(n,Copy(decl),object); } diff --git a/Source/Swig/parms.c b/Source/Swig/parms.c index 2b0691dce..ff8614224 100644 --- a/Source/Swig/parms.c +++ b/Source/Swig/parms.c @@ -30,7 +30,9 @@ Parm *NewParm(SwigType *type, const String_or_char *n) { Parm *p = NewHash(); if (type) { - Setattr(p,"type", Copy(type)); + SwigType *ntype = Copy(type); + Setattr(p,"type",ntype); + Delete(ntype); } Setattr(p,"name",n); return p; @@ -41,41 +43,56 @@ Parm *NewParm(SwigType *type, const String_or_char *n) { * ------------------------------------------------------------------------ */ Parm *CopyParm(Parm *p) { - SwigType *t; - String *name; - String *lname; - String *value; - String *ignore; - String *alttype; - String *byname; - String *compactdefargs; + Parm *np = NewHash(); + SwigType *t = Getattr(p,"type"); + String *name = Getattr(p,"name"); + String *lname = Getattr(p,"lname"); + String *value = Getattr(p,"value"); + String *ignore = Getattr(p,"ignore"); + String *alttype = Getattr(p,"alttype"); + String *byname = Getattr(p, "arg:byname"); + String *compactdefargs = Getattr(p, "compactdefargs"); - Parm *np = NewHash(); - t = Getattr(p,"type"); - name = Getattr(p,"name"); - lname = Getattr(p,"lname"); - value = Getattr(p,"value"); - ignore = Getattr(p,"ignore"); - alttype = Getattr(p,"alttype"); - byname = Getattr(p, "arg:byname"); - compactdefargs = Getattr(p, "compactdefargs"); - - if (t) - Setattr(np,"type",Copy(t)); - if (name) - Setattr(np,"name",Copy(name)); - if (lname) - Setattr(np,"lname", Copy(lname)); - if (value) - Setattr(np,"value", Copy(value)); - if (ignore) - Setattr(np,"ignore", Copy(ignore)); - if (alttype) - Setattr(np,"alttype", Copy(alttype)); - if (byname) - Setattr(np, "arg:byname", Copy(byname)); - if (compactdefargs) - Setattr(np, "compactdefargs", Copy(compactdefargs)); + if (t) { + SwigType *nt = Copy(t); + Setattr(np,"type",nt); + Delete(nt); + } + if (name) { + String *str = Copy(name); + Setattr(np,"name",str); + Delete(str); + } + if (lname) { + String *str = Copy(lname); + Setattr(np,"lname", str); + Delete(str); + } + if (value) { + String *str = Copy(value); + Setattr(np,"value", str); + Delete(str); + } + if (ignore) { + String *str = Copy(ignore); + Setattr(np,"ignore", str); + Delete(str); + } + if (alttype) { + String *str = Copy(alttype); + Setattr(np,"alttype", str); + Delete(str); + } + if (byname) { + String *str = Copy(byname); + Setattr(np, "arg:byname", str); + Delete(str); + } + if (compactdefargs) { + String *str = Copy(compactdefargs); + Setattr(np, "compactdefargs", str); + Delete(str); + } Setfile(np,Getfile(p)); Setline(np,Getline(p)); @@ -99,6 +116,7 @@ CopyParmList(ParmList *p) { np = CopyParm(p); if (pp) { set_nextSibling(pp,np); + Delete(np); } else { fp = np; } @@ -280,6 +298,7 @@ ParmList *ParmList_copy_all_except_last_parm(ParmList *p) { newparm = CopyParm(p); if (pp) { set_nextSibling(pp,newparm); + Delete(newparm); } else { fp = newparm; } diff --git a/Source/Swig/stype.c b/Source/Swig/stype.c index dc889d0aa..125045f61 100644 --- a/Source/Swig/stype.c +++ b/Source/Swig/stype.c @@ -386,6 +386,7 @@ SwigType *SwigType_default(SwigType *t) { def = r; return def; } + Delete(q); } if (Strcmp(r,"p.SWIGTYPE") == 0) { def = NewString("SWIGTYPE"); @@ -502,14 +503,17 @@ SwigType_namestr(const SwigType *t) { p = SwigType_parmlist(t); sz = Len(p); for (i = 0; i < sz; i++) { - Append(r,SwigType_str(Getitem(p,i),0)); + String *str = SwigType_str(Getitem(p,i),0); + Append(r,str); if ((i+1) < sz) Putc(',',r); + Delete(str); } Putc(' ',r); Putc('>',r); suffix = SwigType_templatesuffix(t); Append(r,suffix); Delete(suffix); + Delete(p); #if 0 if (SwigType_istemplate(r)) { SwigType *rr = SwigType_namestr(r); @@ -835,6 +839,7 @@ String *SwigType_rcaststr(SwigType *s, const String_or_char *name) { for (j = 0; j < plen; j++) { p = SwigType_str(Getitem(parms,j),0); Append(result,p); + Delete(p); if (j < (plen-1)) Append(result,","); } Append(result,")"); @@ -886,7 +891,9 @@ String *SwigType_lcaststr(SwigType *s, const String_or_char *name) { if (SwigType_isarray(s)) { Printf(result,"(%s)%s", SwigType_lstr(s,0),name); } else if (SwigType_isreference(s)) { - Printf(result,"(%s)", SwigType_str(s,0)); + String *str = SwigType_str(s,0); + Printf(result,"(%s)", str); + Delete(str); if (name) Printv(result,name,NIL); } else if (SwigType_isqualifier(s)) { @@ -1044,6 +1051,7 @@ SwigType_typename_replace(SwigType *t, String *pat, String *rep) { } Clear(t); Append(t,nt); + Delete(elem); } /* ----------------------------------------------------------------------------- diff --git a/Source/Swig/typemap.c b/Source/Swig/typemap.c index 0b109e43a..c8d97b809 100644 --- a/Source/Swig/typemap.c +++ b/Source/Swig/typemap.c @@ -177,7 +177,7 @@ Swig_typemap_register(const String_or_char *op, ParmList *parms, String_or_char tm1 = Getattr(tm,pname); if (!tm1) { tm1 = NewHash(); - Setattr(tm,NewString(pname),tm1); + Setattr(tm,pname,tm1); Delete(tm1); } tm = tm1; @@ -225,14 +225,25 @@ Swig_typemap_register(const String_or_char *op, ParmList *parms, String_or_char /* Setattr(tm2,newop,newop); */ Delete(newop); } else { - Setattr(tm2,"code",NewString(code)); - Setattr(tm2,"type",Copy(type)); - Setattr(tm2,"typemap",NewStringf("typemap(%s) %s", op, SwigType_str(type,pname))); + String *str = SwigType_str(type,pname); + String *typemap = NewStringf("typemap(%s) %s", op, str); + ParmList *clocals = CopyParmList(locals); + ParmList *ckwargs = CopyParmList(kwargs); + + Setattr(tm2,"code", code); + Setattr(tm2,"type", type); + Setattr(tm2,"typemap", typemap); if (pname) { - Setattr(tm2,"pname", NewString(pname)); + Setattr(tm2,"pname", pname); } - Setattr(tm2,"locals", CopyParmList(locals)); - Setattr(tm2,"kwargs", CopyParmList(kwargs)); + Setattr(tm2,"locals", clocals); + Setattr(tm2,"kwargs", ckwargs); + + Delete(clocals); + Delete(ckwargs); + + Delete(str); + Delete(typemap); } } @@ -1079,14 +1090,19 @@ static void typemap_locals(DOHString *s, ParmList *l, Wrapper *f, int argnum) { continue; } if (value) { - new_name = Wrapper_new_localv(f,str, SwigType_str(pt,str), "=", value, NIL); + String *pstr = SwigType_str(pt,str); + new_name = Wrapper_new_localv(f,str, pstr, "=", value, NIL); + Delete(pstr); } else { - new_name = Wrapper_new_localv(f,str, SwigType_str(pt,str), NIL); + String *pstr = SwigType_str(pt,str); + new_name = Wrapper_new_localv(f,str, pstr, NIL); + Delete(pstr); } if (!isglobal) { /* Substitute */ Replace(s,pn,new_name,DOH_REPLACE_ID | DOH_REPLACE_NOQUOTE); } + Delete(str); } } p = nextSibling(p); diff --git a/Source/Swig/typesys.c b/Source/Swig/typesys.c index 29825d8e7..5acd9ea58 100644 --- a/Source/Swig/typesys.c +++ b/Source/Swig/typesys.c @@ -869,7 +869,8 @@ SwigType *SwigType_typedef_qualified(SwigType *t) String *isenum = 0; if (SwigType_isenum(e)) { isenum = NewString("enum "); - e = NewString(Char(e)+5); + ty = NewString(Char(e)+5); + e = ty; } resolved_scope = 0; if (typedef_resolve(current_scope,e)) { @@ -917,11 +918,10 @@ SwigType *SwigType_typedef_qualified(SwigType *t) } } } - if (isenum) { Insert(e,0,isenum); + Delete(isenum); } - } else { /* Template. We need to qualify template parameters as well as the template itself */ String *tprefix, *qprefix; @@ -1289,7 +1289,12 @@ SwigType *SwigType_alttype(SwigType *t, int local_tmap) { use_wrapper = 1; } } - if (use_wrapper) w = NewStringf("SwigValueWrapper<%s >",SwigType_str(td,0)); + if (use_wrapper) { + String *str = SwigType_str(td,0); + w = NewStringf("SwigValueWrapper<%s >",str); + Delete(str); + } + Delete(td); } return w; @@ -1414,6 +1419,7 @@ void SwigType_remember_clientdata(SwigType *t, const String_or_char *clientdata) lt = SwigType_ltype(t); } Setattr(r_ltype, mt, lt); + Delete(lt); fr = SwigType_typedef_resolve_all(t); /* Create fully resolved type */ qr = SwigType_typedef_qualified(fr); Delete(fr); @@ -1455,7 +1461,9 @@ void SwigType_remember_clientdata(SwigType *t, const String_or_char *clientdata) assert(0); } } else { - Setattr(r_clientdata, fr, NewString(clientdata)); + String *cstr = NewString(clientdata); + Setattr(r_clientdata, fr, cstr); + Delete(cstr); } } @@ -1588,18 +1596,22 @@ static Hash *conversions = 0; void SwigType_inherit(String *derived, String *base, String *cast) { Hash *h; + String *dd = 0; + String *bb = 0; if (!subclass) subclass = NewHash(); /* Printf(stdout,"'%s' --> '%s' '%s'\n", derived, base, cast); */ if (SwigType_istemplate(derived)) { String *ty = SwigType_typedef_resolve_all(derived); - derived = SwigType_typedef_qualified(ty); + dd = SwigType_typedef_qualified(ty); + derived = dd; Delete(ty); } if (SwigType_istemplate(base)) { String *ty = SwigType_typedef_resolve_all(base); - base = SwigType_typedef_qualified(ty); + bb = SwigType_typedef_qualified(ty); + base = bb; Delete(ty); } @@ -1614,6 +1626,8 @@ SwigType_inherit(String *derived, String *base, String *cast) { Setattr(h,derived, cast ? cast : (void *) ""); } + Delete(dd); + Delete(bb); } /* ----------------------------------------------------------------------------- @@ -1659,6 +1673,7 @@ SwigType_issubtype(SwigType *t1, SwigType *t2) { void SwigType_inherit_equiv(File *out) { String *ckey; String *prefix, *base; + String *mprefix, *mkey; Hash *sub; Hash *rh; List *rlist; @@ -1694,15 +1709,21 @@ void SwigType_inherit_equiv(File *out) { prefix= SwigType_prefix(rk.key); Append(prefix,bk.key); /* Printf(stdout,"set %x = '%s' : '%s'\n", rh, SwigType_manglestr(prefix),prefix); */ - Setattr(rh,SwigType_manglestr(prefix),prefix); - ckey = NewStringf("%s+%s",SwigType_manglestr(prefix), SwigType_manglestr(rk.key)); + mprefix = SwigType_manglestr(prefix); + Setattr(rh,mprefix,prefix); + mkey = SwigType_manglestr(rk.key); + ckey = NewStringf("%s+%s",mprefix, mkey); if (!Getattr(conversions,ckey)) { - String *convname = NewStringf("%sTo%s", SwigType_manglestr(prefix), SwigType_manglestr(rk.key)); + String *convname = NewStringf("%sTo%s", mprefix, mkey); + String *lkey = SwigType_lstr(rk.key,0); + String *lprefix = SwigType_lstr(prefix,0); Printf(out,"static void *%s(void *x) {\n", convname); - Printf(out," return (void *)((%s) %s ((%s) x));\n", SwigType_lstr(rk.key,0), Getattr(sub,bk.key), SwigType_lstr(prefix,0)); + Printf(out," return (void *)((%s) %s ((%s) x));\n", lkey, Getattr(sub,bk.key), lprefix); Printf(out,"}\n"); Setattr(conversions,ckey,convname); Delete(ckey); + Delete(lkey); + Delete(lprefix); /* This inserts conversions for typedefs */ { @@ -1716,7 +1737,7 @@ void SwigType_inherit_equiv(File *out) { /* Make sure this name equivalence is not due to inheritance */ if (Cmp(prefix, Getattr(r,rrk.key)) == 0) { - rkeymangle = SwigType_manglestr(rk.key); + rkeymangle = Copy(mkey); ckey = NewStringf("%s+%s", rrk.key, rkeymangle); if (!Getattr(conversions, ckey)) { Setattr(conversions, ckey, convname); @@ -1739,6 +1760,8 @@ void SwigType_inherit_equiv(File *out) { Delete(convname); } Delete(prefix); + Delete(mprefix); + Delete(mkey); bk = Next(bk); } rk = Next(rk);