From 39559657395fd29c96a3e6a1cfd142de52890d9c Mon Sep 17 00:00:00 2001 From: Marcelo Matus Date: Sun, 4 Dec 2005 01:16:57 +0000 Subject: [PATCH] more memory leaks fixes git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@7921 626c5289-ae23-0410-ae9c-e8d60b6d4f22 --- Lib/python/pyinit.swg | 7 ++--- Source/CParse/parser.y | 40 +++++++++++++++++++++----- Source/CParse/util.c | 1 + Source/DOH/memory.c | 3 +- Source/DOH/string.c | 39 ++++++++++++------------- Source/Modules/python.cxx | 60 +++++++++++++-------------------------- Source/Modules/ruby.cxx | 3 ++ Source/Swig/stype.c | 16 ++++++----- Source/Swig/typemap.c | 16 ++++++++--- Source/Swig/typesys.c | 4 +++ Source/Swig/wrapfunc.c | 17 ++++++----- 11 files changed, 112 insertions(+), 94 deletions(-) diff --git a/Lib/python/pyinit.swg b/Lib/python/pyinit.swg index e579f41b4..659ff300c 100644 --- a/Lib/python/pyinit.swg +++ b/Lib/python/pyinit.swg @@ -32,8 +32,7 @@ typedef struct swig_varlinkobject { } swig_varlinkobject; SWIGINTERN PyObject * -swig_varlink_repr(swig_varlinkobject *v) { - v = v; +swig_varlink_repr(swig_varlinkobject *SWIGUNUSEDPARM(v)) { return PyString_FromString(""); } @@ -50,7 +49,7 @@ swig_varlink_print(swig_varlinkobject *v, FILE *fp, int SWIGUNUSEDPARM(flags)) { } SWIGINTERN void -swig_varlink_delete(swig_varlinkobject *v) { +swig_varlink_dealloc(swig_varlinkobject *v) { swig_globalvar *var = v->vars; while (var) { swig_globalvar *n = var->next; @@ -102,7 +101,7 @@ swig_varlink_type(void) { (char *)"swigvarlink", /* Type name (tp_name) */ sizeof(swig_varlinkobject), /* Basic size (tp_basicsize) */ 0, /* Itemsize (tp_itemsize) */ - (destructor) swig_varlink_delete, /* Deallocator (tp_dealloc) */ + (destructor) swig_varlink_dealloc, /* Deallocator (tp_dealloc) */ (printfunc) swig_varlink_print, /* Print (tp_print) */ (getattrfunc) swig_varlink_getattr, /* get attr (tp_getattr) */ (setattrfunc) swig_varlink_setattr, /* Set attr (tp_setattr) */ diff --git a/Source/CParse/parser.y b/Source/CParse/parser.y index 7a2705976..26f958df7 100644 --- a/Source/CParse/parser.y +++ b/Source/CParse/parser.y @@ -786,6 +786,7 @@ static void merge_extensions(Node *cls, Node *am) { qargs = Swig_symbol_type_qualify(args,0); Append(prefix,qargs); Delete(nname); + Delete(args); Delete(qargs); nname = prefix; } @@ -960,12 +961,14 @@ static String *resolve_node_scope(String *cname) { if (nscope_inner) { if (Getattr(nscope_inner,"symtab") != Getattr(ns2,"symtab")) { appendChild(nscope_inner,ns2); + Delete(ns2); } } nscope_inner = ns2; if (!nscope) nscope = ns2; } cname = base; + Delete(scopes); } } Delete(prefix); @@ -1039,6 +1042,7 @@ static Node *dump_nested(const char *parent) { add_symbols(retx); if (ret) { set_nextSibling(retx,ret); + Delete(ret); } ret = retx; @@ -1101,6 +1105,7 @@ static Node *dump_nested(const char *parent) { Setattr(head,"code", code); Delete(code); set_nextSibling(head,ret); + Delete(ret); ret = head; } @@ -1230,7 +1235,9 @@ static void default_arguments(Node *n) { SwigType *t = Getattr(p,"type"); if (Strcmp(t,"v(...)") == 0) { if (pp) { - set_nextSibling(pp,Copy(varargs)); + ParmList *cv = Copy(varargs); + set_nextSibling(pp,cv); + Delete(cv); } else { ParmList *cv = Copy(varargs); Setattr(function,"parms", cv); @@ -1333,7 +1340,7 @@ static void default_arguments(Node *n) { /* Point to the new function, extending the linked list */ set_nextSibling(function, new_function); - + Delete(new_function); function = new_function; Delete(ntype); @@ -1505,7 +1512,8 @@ program : interface { top = $1; } | PARSETYPE parm SEMI { - top = Getattr($2,"type"); + top = Copy(Getattr($2,"type")); + Delete($2); } | PARSETYPE error { top = 0; @@ -1895,6 +1903,7 @@ include_directive: includetype options string LBRACKET { Node *mnode = new_node("module"); Setattr(mnode,"name", mname); appendChild(nint,mnode); + Delete(mnode); appendChild(nint,firstChild($$)); $$ = nint; Setattr($$,"module",mname); @@ -2325,6 +2334,7 @@ varargs_parms : parms { $$ = $1; } for (i = 0; i < n; i++) { p = Copy($3); set_nextSibling(p,$$); + Delete($$); $$ = p; } } @@ -2695,6 +2705,7 @@ template_directive: SWIGTEMPLATE LPAREN idstringopt RPAREN idcolonnt LESSTHAN va /* non-global namespace */ if (templnode) { appendChild(nscope_inner,templnode); + Delete(templnode); if (nscope) $$ = nscope; } } else { @@ -2703,6 +2714,7 @@ template_directive: SWIGTEMPLATE LPAREN idstringopt RPAREN idcolonnt LESSTHAN va $$ = templnode; } else { set_nextSibling(linklistend,templnode); + Delete(templnode); } linklistend = templnode; } @@ -2814,7 +2826,9 @@ c_decl : storage_class type declarator initializer c_decl_tail { if (p) { if ((Namespaceprefix && Strcmp(p,Namespaceprefix) == 0) || (inclass && Strcmp(p,Classprefix) == 0)) { - Setattr($$,"name",Swig_scopename_last($3.id)); + String *lstr = Swig_scopename_last($3.id); + Setattr($$,"name",lstr); + Delete(lstr); set_nextSibling($$,$5); } else { Delete($$); @@ -2993,6 +3007,7 @@ c_enum_decl : storage_class ENUM ename LBRACE enumlist RBRACE SEMI { add_symbols($$); /* Add enum to tag space */ set_nextSibling($$,n); + Delete(n); add_symbols($5); /* Add enum values to id space */ add_symbols(n); @@ -3236,6 +3251,7 @@ cpp_class_decl : Setattr(pa,"kind","public"); cplus_mode = CPLUS_PUBLIC; appendChild($$,pa); + Delete(pa); } Setattr($$,"symtab",Swig_symbol_popscope()); @@ -3321,6 +3337,7 @@ cpp_class_decl : } } set_nextSibling($$,n); + Delete(n); { /* If a proper typedef name was given, we'll use it to set the scope name */ String *name = 0; @@ -3584,6 +3601,7 @@ cpp_template_decl : TEMPLATE LESSTHAN template_parms GREATERTHAN { template_para Delete(rtt); Delete(ttr); } + Delete(tparms); Append(ffname,")>"); } { @@ -3591,6 +3609,7 @@ cpp_template_decl : TEMPLATE LESSTHAN template_parms GREATERTHAN { template_para if (!partials) { partials = NewList(); Setattr(tempn,"partials",partials); + Delete(partials); } /* Printf(stdout,"partial: fname = '%s', '%s'\n", fname, Swig_symbol_typedef_reduce(fname,0)); */ Append(partials,ffname); @@ -5388,9 +5407,15 @@ base_list : base_specifier { Hash *list = NewHash(); Node *base = $1; Node *name = Getattr(base,"name"); - Setattr(list,"public",NewList()); - Setattr(list,"protected",NewList()); - Setattr(list,"private",NewList()); + List *lpublic = NewList(); + List *lprotected = NewList(); + List *lprivate = NewList(); + Setattr(list,"public",lpublic); + Setattr(list,"protected",lprotected); + Setattr(list,"private",lprivate); + Delete(lpublic); + Delete(lprotected); + Delete(lprivate); Append(Getattr(list,Getattr(base,"access")),name); $$ = list; } @@ -5742,6 +5767,7 @@ Parm *Swig_cparse_parm(String *s) { scanner_next_token(PARSEPARM); yyparse(); /* Printf(stdout,"typeparse: '%s' ---> '%s'\n", s, top); */ + Delete(ns); return top; } diff --git a/Source/CParse/util.c b/Source/CParse/util.c index c315f5fd6..daec85c9d 100644 --- a/Source/CParse/util.c +++ b/Source/CParse/util.c @@ -61,6 +61,7 @@ void Swig_cparse_replace_descriptor(String *s) { Replace(s,tmp,descriptor,DOH_REPLACE_ANY); Delete(mangle); Delete(descriptor); + Delete(t); } else { Swig_error(Getfile(s),Getline(s),"Bad $descriptor() macro.\n"); break; diff --git a/Source/DOH/memory.c b/Source/DOH/memory.c index b5717b297..ca23fcdf5 100644 --- a/Source/DOH/memory.c +++ b/Source/DOH/memory.c @@ -115,11 +115,10 @@ DohObjMalloc(DohObjInfo *type, void *data) { FreeList = (DohBase *) obj->data; } else { while (Pools->current == Pools->len) { - PoolSize *= 2; CreatePool(); } obj = Pools->ptr + Pools->current; - Pools->current++; + ++Pools->current; } obj->type = type; obj->data = data; diff --git a/Source/DOH/string.c b/Source/DOH/string.c index 6611cf234..da4211465 100644 --- a/Source/DOH/string.c +++ b/Source/DOH/string.c @@ -295,9 +295,10 @@ String_insert(DOH *so, int pos, DOH *str) /* See if there is room to insert the new data */ while (s->maxsize <= s->len+len) { - s->str = (char *) DohRealloc(s->str,2*s->maxsize); + int newsize = 2*s->maxsize; + s->str = (char *) DohRealloc(s->str,newsize); assert(s->str); - s->maxsize *= 2; + s->maxsize = newsize; } memmove(s->str+pos+len, s->str+pos, (s->len - pos)); memcpy(s->str+pos,data,len); @@ -502,31 +503,27 @@ String_tell(DOH *so) int DohString_putc(DOH *so, int ch) { - register int len, maxsize, sp; String *s = (String *) ObjData(so); + register int len = s->len; + register int sp = s->sp; s->hashkey = -1; - len = s->len; - sp = s->sp; if (sp >= len) { - register char *tc; - maxsize = s->maxsize; + register size_t maxsize = s->maxsize; + register char *tc = s->str; if (len > (maxsize-2)) { - s->str = (char *) DohRealloc(s->str,2*maxsize); - assert(s->str); - s->maxsize = 2*maxsize; + maxsize *= 2; + tc = (char *) DohRealloc(tc, maxsize); + assert(tc); + s->maxsize = (int)maxsize; + s->str = tc; } - tc = s->str + len; - *(tc++) = ch; - if (sp >= len) { - s->sp = len+1; - *tc = 0; - if (ch == '\n') s->line++; - } - s->len = len+1; + tc += sp; + *tc = (char) ch; *(++tc) = 0; + s->len = s->sp = sp + 1; } else { s->str[s->sp++] = (char) ch; - if (ch == '\n') s->line++; } + if (ch == '\n') s->line++; return ch; } @@ -543,7 +540,7 @@ DohString_getc(DOH *so) c = EOF; else c = (int) s->str[s->sp++]; - if (c == '\n') s->line++; + if (c == '\n') s->line--; return c; } @@ -558,7 +555,7 @@ DohString_ungetc(DOH *so, int ch) if (ch == EOF) return ch; if (s->sp <= 0) return EOF; s->sp--; - if (ch == '\n') s->line--; + if (ch == '\n') s->line++; return ch; } diff --git a/Source/Modules/python.cxx b/Source/Modules/python.cxx index d76f1df94..a37af07d8 100644 --- a/Source/Modules/python.cxx +++ b/Source/Modules/python.cxx @@ -1220,29 +1220,15 @@ public: if (!allow_kwargs || Getattr(n,"sym:overloaded")) { if (!varargs) { - if (CPlusPlus) { - Printv(f->def, - "SWIGINTERN PyObject *", wname, - "(PyObject *, PyObject *args) {", - NIL); - } else { - Printv(f->def, - "SWIGINTERN PyObject *", wname, - "(PyObject *self, PyObject *args) {", - NIL); - } + Printv(f->def, + "SWIGINTERN PyObject *", wname, + "(PyObject *SWIGUNUSEDPARM(self), PyObject *args) {", + NIL); } else { - if (CPlusPlus) { - Printv(f->def, - "SWIGINTERN PyObject *", wname, "__varargs__", - "(PyObject *, PyObject *args, PyObject *varargs) {", - NIL); - } else { - Printv(f->def, - "SWIGINTERN PyObject *", wname, "__varargs__", - "(PyObject *self, PyObject *args, PyObject *varargs) {", - NIL); - } + Printv(f->def, + "SWIGINTERN PyObject *", wname, "__varargs__", + "(PyObject *SWIGUNUSEDPARM(self), PyObject *args, PyObject *varargs) {", + NIL); } if (allow_kwargs) { Swig_warning(WARN_LANG_OVERLOAD_KEYWORD, input_file, line_number, @@ -1255,17 +1241,10 @@ public: "Can't wrap varargs with keyword arguments enabled\n"); varargs = 0; } - if (CPlusPlus) { - Printv(f->def, - "SWIGINTERN PyObject *", wname, - "(PyObject *, PyObject *args, PyObject *kwargs) {", - NIL); - } else { - Printv(f->def, - "SWIGINTERN PyObject *", wname, - "(PyObject *self, PyObject *args, PyObject *kwargs) {", - NIL); - } + Printv(f->def, + "SWIGINTERN PyObject *", wname, + "(PyObject *SWIGUNUSEDPARM(self), PyObject *args, PyObject *kwargs) {", + NIL); } if (!allow_kwargs) { Printf(parse_args," if(!PyArg_ParseTuple(args,(char *)\""); @@ -1549,6 +1528,7 @@ public: if ((tm = Swig_typemap_lookup_new("newfree",n,"result",0))) { Replaceall(tm,"$source","result"); Printf(f->code,"%s\n",tm); + Delete(tm); } } @@ -1556,6 +1536,7 @@ public: if ((tm = Swig_typemap_lookup_new("ret", n, "result", 0))) { Replaceall(tm,"$source","result"); Printf(f->code,"%s\n",tm); + Delete(tm); } if (director_method) { @@ -2158,13 +2139,8 @@ public: { SwigType *ct = NewStringf("p.%s", real_classname); SwigType_remember(ct); - if (CPlusPlus) { - Printv(f_wrappers, - "SWIGINTERN PyObject * ", class_name, "_swigregister(PyObject *, PyObject *args) {\n", NIL); - } else { - Printv(f_wrappers, - "SWIGINTERN PyObject * ", class_name, "_swigregister(PyObject *self SWIGUNUSED, PyObject *args) {\n", NIL); - } + Printv(f_wrappers, + "SWIGINTERN PyObject * ", class_name, "_swigregister(PyObject *SWIGUNUSEDPARM(self), PyObject *args) {\n", NIL); Printv(f_wrappers, tab4, "PyObject *obj;\n", tab4, "if (!PyArg_UnpackTuple(args,(char*)\"swigregister\", 1, 1,&obj)) return NULL;\n", @@ -2297,6 +2273,7 @@ public: Replaceall(pycode,"$action", pyaction); Delete(pyaction); Printv(f_shadow,pycode,"\n",NIL); + Delete(pycode); } else { if (!have_addtofunc(n)) { if (!allow_kwargs) { @@ -2428,6 +2405,7 @@ public: Replaceall(pycode,"$action", pyaction); Delete(pyaction); Printv(f_shadow,pycode,"\n",NIL); + Delete(pycode); } else { String *pass_self = NewString(""); Node *parent = Swig_methodclass(n); @@ -2482,6 +2460,7 @@ public: Replaceall(pycode,"$action", pyaction); Delete(pyaction); Printv(f_shadow_stubs,pycode,"\n",NIL); + Delete(pycode); } else { Printv(f_shadow_stubs, "\ndef ", symname, "(*args", @@ -2524,6 +2503,7 @@ public: Replaceall(pycode, "$action", pyaction); Delete(pyaction); Printv(f_shadow,pycode,"\n", NIL); + Delete(pycode); } else { Printv(f_shadow, tab4, "__swig_destroy__ = ", module, ".", Swig_name_destroy(symname), "\n", NIL); if (!have_pythonprepend(n) && !have_pythonappend(n)) { diff --git a/Source/Modules/ruby.cxx b/Source/Modules/ruby.cxx index 521736d3a..23fbe9ed3 100644 --- a/Source/Modules/ruby.cxx +++ b/Source/Modules/ruby.cxx @@ -1317,6 +1317,7 @@ public: #else Printf(f->code,"%s\n", tm); #endif + Delete(tm); } else { Swig_warning(WARN_TYPEMAP_OUT_UNDEF, input_file, line_number, "Unable to use return type %s.\n", SwigType_str(t,0)); @@ -1352,6 +1353,7 @@ public: if (tm) { Replaceall(tm,"$source","result"); Printv(f->code,tm, "\n",NIL); + Delete(tm); } } @@ -1360,6 +1362,7 @@ public: if (tm) { Replaceall(tm,"$source","result"); Printv(f->code,tm, NIL); + Delete(tm); } if (director_method) { diff --git a/Source/Swig/stype.c b/Source/Swig/stype.c index de10b4cae..ce2ee4e47 100644 --- a/Source/Swig/stype.c +++ b/Source/Swig/stype.c @@ -364,7 +364,6 @@ SwigType *SwigType_default(SwigType *t) { r = Getattr(default_cache,t); if (r) { - if (Strcmp(r,t) == 0) return 0; return Copy(r); } #endif @@ -467,17 +466,20 @@ SwigType *SwigType_default(SwigType *t) { def = NewString("SWIGTYPE"); } if (r != t) Delete(r); -#ifdef SWIG_DEFAULT_CACHE - /* The cache produces strange results, see enum_template.i case */ - cdef = Copy(def); - Setattr(default_cache,t, cdef); - Delete(cdef); -#endif if (StringEqual(def,t)) { Delete(def); def = 0; } +#ifdef SWIG_DEFAULT_CACHE + /* The cache produces strange results, see enum_template.i case */ + if (def) { + cdef = Copy(def); + Setattr(default_cache,t, cdef); + Delete(cdef); + } +#endif + /* Printf(stderr,"type : def %s : %s\n", t, def); */ return def; diff --git a/Source/Swig/typemap.c b/Source/Swig/typemap.c index beb71c438..4889b0900 100644 --- a/Source/Swig/typemap.c +++ b/Source/Swig/typemap.c @@ -1111,10 +1111,12 @@ static void typemap_locals(DOHString *s, ParmList *l, Wrapper *f, int argnum) { if ((argnum >= 0) && (!isglobal)) { Printf(str,"%s%d",pn,argnum); } else { - Printf(str,"%s",pn); + StringAppend(str,pn); } if (isglobal && Wrapper_check_local(f,str)) { p = nextSibling(p); + Delete(str); + if (at) Delete(at); continue; } if (value) { @@ -1134,7 +1136,7 @@ static void typemap_locals(DOHString *s, ParmList *l, Wrapper *f, int argnum) { } } p = nextSibling(p); - Delete(at); + if (at) Delete(at); } } @@ -1156,7 +1158,11 @@ String *Swig_typemap_lookup(const String_or_char *op, SwigType *type, String_or_ if (!tm) return 0; s = Getattr(tm,k_code); - if (!s) return 0; + if (!s) { + if (mtype) Delete(mtype); + return 0; + } + /* Blocked */ if (Cmp(s,"pass") == 0) { @@ -1342,7 +1348,7 @@ Printf(stdout, "Swig_typemap_lookup %s [%s %s]\n", op, type, pname ? pname : "NO if (type) { SwigType *rtype = SwigType_typedef_resolve_all(type); String *mangle = Swig_string_mangle(rtype); - Printf(value,"%s",mangle); + StringAppend(value,mangle); Delete(mangle); Delete(rtype); } @@ -1678,6 +1684,8 @@ static void replace_embedded_typemap(String *s) { Insert(n,0,"$"); Setattr(vars,n,v); } + Delete(n); + Delete(v); } method = Getitem(l,0); diff --git a/Source/Swig/typesys.c b/Source/Swig/typesys.c index b45ed7c6e..121e0cf5a 100644 --- a/Source/Swig/typesys.c +++ b/Source/Swig/typesys.c @@ -292,6 +292,7 @@ SwigType_inherit_scope(Typetab *scope) { if (!inherits) { inherits = NewList(); Setattr(current_scope,k_inherit, inherits); + Delete(inherits); } assert(scope != current_scope); @@ -338,6 +339,7 @@ SwigType_using_scope(Typetab *scope) { if (!ulist) { ulist = NewList(); Setattr(current_scope,k_using, ulist); + Delete(ulist); } assert(scope != current_scope); len = Len(ulist); @@ -1645,6 +1647,7 @@ SwigType_inherit(String *derived, String *base, String *cast) { if (!h) { h = NewHash(); Setattr(subclass,base,h); + Delete(h); } if (!Getattr(h,derived)) { Setattr(h,derived, cast ? cast : (void *) ""); @@ -1789,6 +1792,7 @@ void SwigType_inherit_equiv(File *out) { bk = Next(bk); } rk = Next(rk); + Delete(rlist); } } diff --git a/Source/Swig/wrapfunc.c b/Source/Swig/wrapfunc.c index f77f65e9d..a739aa62b 100644 --- a/Source/Swig/wrapfunc.c +++ b/Source/Swig/wrapfunc.c @@ -83,7 +83,6 @@ Wrapper_pretty_print(String *str, File *f) { ts = NewStringEmpty(); Seek(str,0, SEEK_SET); - Clear(ts); while ((c = Getc(str)) != EOF) { if (c == '\"') { Putc(c,ts); @@ -222,8 +221,6 @@ Wrapper_compact_print(String *str, File *f) { ts = NewStringEmpty(); tf = NewStringEmpty(); Seek(str,0, SEEK_SET); - Clear(ts); - Clear(tf); while ((c = Getc(str)) != EOF) { if (c == '\"') { /* string 1 */ @@ -263,7 +260,7 @@ Wrapper_compact_print(String *str, File *f) { for (i = 0; i < level; i++) Putc(' ',tf); } - Printf(tf,"%s",ts); + Append(tf,ts); Clear(ts); level+=indent; while ((c = Getc(str)) != EOF) { @@ -286,7 +283,7 @@ Wrapper_compact_print(String *str, File *f) { for (i = 0; i < level; i++) Putc(' ',tf); } - Printf(tf, "%s", ts); + Append(tf, ts); Putc(c, tf); Clear(ts); level-=indent; @@ -312,7 +309,7 @@ Wrapper_compact_print(String *str, File *f) { for (i = 0; i < level; i++) Putc(' ',tf); } - Printf(tf,"%s",ts); + Append(tf,ts); Clear(ts); } Ungetc(c,str); @@ -356,9 +353,9 @@ Wrapper_compact_print(String *str, File *f) { break; } if (!empty) { - Printf(tf,"\n"); + Append(tf,"\n"); } - Printf(tf,"%s",ts); + Append(tf,ts); Printf(f, "%s", tf); Clear(tf); Clear(ts); @@ -373,7 +370,7 @@ Wrapper_compact_print(String *str, File *f) { } } if (!empty) { - Printf(tf,"%s",ts); + Append(tf,ts); } if (Len(tf) != 0) Printf(f,"%s",tf); @@ -400,6 +397,8 @@ Wrapper_print(Wrapper *w, File *f) { Wrapper_compact_print(str,f); else Wrapper_pretty_print(str,f); + + Delete(str); } /* -----------------------------------------------------------------------------