From: kramm Date: Wed, 7 Apr 2004 20:15:34 +0000 (+0000) Subject: memory management and logging fixes X-Git-Tag: stable_core_1~7 X-Git-Url: http://git.asbjorn.biz/?p=swftools.git;a=commitdiff_plain;h=263d494bd93334ec04c81dcbe47b69675fa23007 memory management and logging fixes --- diff --git a/lib/python/SWF.c b/lib/python/SWF.c index fd7c423..5f03825 100644 --- a/lib/python/SWF.c +++ b/lib/python/SWF.c @@ -106,12 +106,12 @@ static PyObject* f_load(PyObject* self, PyObject* args) mylog("+%08x(%d) f_load\n", (int)swf, swf->ob_refcnt); memset(&swf->swf, 0, sizeof(SWF)); - swf->filename = strdup(filename); if(!filename) { PyErr_SetString(PyExc_Exception, setError("Couldn't open file %s", filename)); return 0; } + swf->filename = strdup(filename); fi = open(filename,O_RDONLY|O_BINARY); if (fi<0) { PyErr_SetString(PyExc_Exception, setError("Couldn't open file %s", filename)); @@ -143,7 +143,7 @@ static PyObject * swf_save(PyObject* self, PyObject* args, PyObject* kwargs) int fi; char*filename = 0; int compress = 0; - + if(!self) return NULL; @@ -198,9 +198,23 @@ static PyObject * swf_save(PyObject* self, PyObject* args, PyObject* kwargs) } close(fi); - /* TODO: why is this segfaulting?? */ - /* swf_FreeTags(swf); - swf->firstTag = 0;*/ + //swf_FreeTags(swf); + /*{ TAG * t = swf->firstTag; + while (t) + { + mylog("tag: %08x\n",t); + mylog(" id: %d (%s)\n", t->id, swf_TagGetName(t)); + mylog(" data: %08x (%d bytes)\n", t->data, t->len); + mylog(" next: %08x\n", t->next); + TAG * tnew = t->next; + mylog("->free data\n"); + if (t->data) free(t->data); + mylog("->free tag\n"); + free(t); + t = tnew; + } + }*/ + swf->firstTag = 0; return PY_NONE; } @@ -214,6 +228,7 @@ static PyObject * swf_writeCGI(PyObject* self, PyObject* args) if(!swf->swf.firstTag) return NULL; swf_WriteCGI(&swf->swf); + swf_FreeTags(&swf->swf); swf->swf.firstTag = 0; return PY_NONE; } @@ -330,10 +345,8 @@ static int swf_setattr(PyObject * self, char* a, PyObject * o) return 0; } else if(!strcmp(a, "tags")) { PyObject* taglist; - /*if (!PyArg_Parse(o, "O!", &TagListClass, &taglist)); - goto err;*/ - // TODO: check if it's really a taglist taglist = o; + PY_ASSERT_TYPE(taglist,&TagListClass); Py_DECREF(swf->taglist); swf->taglist = taglist; Py_INCREF(swf->taglist); @@ -398,7 +411,7 @@ static PyMethodDef LoggingMethods[] = {"verbose", (PyCFunction)module_verbose, METH_KEYWORDS, "Set the module verbosity"}, {0,0,0,0} }; - + void initSWF(void) { PyObject*module; @@ -416,4 +429,9 @@ void initSWF(void) all_methods = addMethods(all_methods, LoggingMethods); module = Py_InitModule("SWF", all_methods); + + /* Python doesn't copy the PyMethodDef struct, so we need + to keep it around */ + // free(all_methods) } + diff --git a/lib/python/mypython.c b/lib/python/mypython.c index eacd669..cac8b4d 100644 --- a/lib/python/mypython.c +++ b/lib/python/mypython.c @@ -1,5 +1,10 @@ +#include +#include +#include #include + int main(int argn, char*argv[]) { + void* dummy = malloc(4096); return Py_Main(argn, argv); } diff --git a/lib/python/primitives.c b/lib/python/primitives.c index b1cb0c6..9eae2de 100644 --- a/lib/python/primitives.c +++ b/lib/python/primitives.c @@ -41,6 +41,7 @@ PyObject* f_Color(PyObject* self, PyObject* args, PyObject* kwargs) if (!PyArg_ParseTupleAndKeywords(args, kwargs, "iii|i", kwlist, &r,&g,&b,&a)) return NULL; color = PyObject_New(ColorObject, &ColorClass); + mylog("+%08x(%d) color_new(%d,%d,%d,%d)\n", (int)color, color->ob_refcnt, r,g,b,a); color->rgba.r = r; color->rgba.g = g; color->rgba.b = b; @@ -92,6 +93,11 @@ RGBA color_getRGBA(PyObject*self) } return color->rgba; } +void color_dealloc(PyObject* self) +{ + mylog("-%08x(%d) color_dealloc\n", (int)self, self->ob_refcnt); + PyObject_Del(self); +} PyTypeObject ColorClass = { PyObject_HEAD_INIT(NULL) @@ -99,7 +105,7 @@ PyTypeObject ColorClass = tp_name: "Color", tp_basicsize: sizeof(ColorObject), tp_itemsize: 0, - tp_dealloc: dummy_dealloc, + tp_dealloc: color_dealloc, tp_print: 0, tp_getattr: color_getattr, tp_setattr: color_setattr, @@ -121,6 +127,7 @@ PyObject* f_BBox(PyObject* self, PyObject* args, PyObject* kwargs) &box.xmax, &box.ymax)); return NULL; + mylog("+%08x(%d) bbox_new(%d,%d,%d,%d)\n", (int)self, self->ob_refcnt, box.xmin, box.ymin, box.xmax,box.ymax); bbox = PyObject_New(BBoxObject, &BBoxClass); bbox->bbox = box; return (PyObject*)bbox; @@ -159,6 +166,11 @@ err: mylog("swf_setattr %08x(%d) %s = ? (%08x)\n", (int)self, self->ob_refcnt, a, o); return 1; } +void bbox_dealloc(PyObject* self) +{ + mylog("-%08x(%d) bbox_dealloc\n", (int)self, self->ob_refcnt); + PyObject_Del(self); +} SRECT bbox_getBBox(PyObject*self) { BBoxObject*bbox= 0; @@ -177,7 +189,7 @@ PyTypeObject BBoxClass = tp_name: "BBox", tp_basicsize: sizeof(BBoxObject), tp_itemsize: 0, - tp_dealloc: dummy_dealloc, + tp_dealloc: bbox_dealloc, tp_print: 0, tp_getattr: bbox_getattr, tp_setattr: bbox_setattr, @@ -224,6 +236,11 @@ MATRIX matrix_getMatrix(PyObject*self) MatrixObject*matrix = (MatrixObject*)self; return matrix->matrix; } +void matrix_dealloc(PyObject* self) +{ + mylog("-%08x(%d) matrix_dealloc", self, self->ob_refcnt); + PyObject_Del(self); +} PyTypeObject MatrixClass = { PyObject_HEAD_INIT(NULL) @@ -231,7 +248,7 @@ PyTypeObject MatrixClass = tp_name: "Matrix", tp_basicsize: sizeof(MatrixObject), tp_itemsize: 0, - tp_dealloc: dummy_dealloc, + tp_dealloc: matrix_dealloc, tp_print: 0, tp_getattr: matrix_getattr, tp_setattr: matrix_setattr, @@ -273,6 +290,11 @@ CXFORM colortransform_getCXForm(PyObject*self) } return cxform->cxform; } +void colortransform_dealloc(PyObject* self) +{ + mylog("-%08x(%d) colortransform_dealloc", self, self->ob_refcnt); + PyObject_Del(self); +} PyTypeObject CXFormClass = { PyObject_HEAD_INIT(NULL) @@ -280,7 +302,7 @@ PyTypeObject CXFormClass = tp_name: "ColorTransform", tp_basicsize: sizeof(CXFormObject), tp_itemsize: 0, - tp_dealloc: dummy_dealloc, + tp_dealloc: colortransform_dealloc, tp_print: 0, tp_getattr: colortransform_getattr, tp_setattr: colortransform_setattr, @@ -303,7 +325,7 @@ static int gradient_setattr(PyObject * self, char* a, PyObject* o) { return 0; } -GRADIENT colortransform_getGradient(PyObject*self) +GRADIENT gradient_getGradient(PyObject*self) { GradientObject*gradient = 0; if (!PyArg_Parse(self, "O!", &gradient, &gradient)) { @@ -314,6 +336,11 @@ GRADIENT colortransform_getGradient(PyObject*self) } return gradient->gradient; } +void gradient_dealloc(PyObject* self) +{ + mylog("-%08x(%d) gradient_dealloc", self, self->ob_refcnt); + PyObject_Del(self); +} PyTypeObject GradientClass = { PyObject_HEAD_INIT(NULL) @@ -321,7 +348,7 @@ PyTypeObject GradientClass = tp_name: "Gradient", tp_basicsize: sizeof(GradientObject), tp_itemsize: 0, - tp_dealloc: dummy_dealloc, + tp_dealloc: gradient_dealloc, tp_print: 0, tp_getattr: gradient_getattr, tp_setattr: gradient_setattr, diff --git a/lib/python/pyutils.c b/lib/python/pyutils.c index 95c222b..f926bd7 100644 --- a/lib/python/pyutils.c +++ b/lib/python/pyutils.c @@ -36,8 +36,8 @@ void mylog(char*format, ...) buf[l-1] = 0; l--; } - fprintf(stdout, "[SWF] %s\n", buf); - fflush(stdout); + fprintf(stderr, "[SWF] %s\n", buf); + fflush(stderr); } #define PY_NONE Py_BuildValue("s", 0) @@ -70,9 +70,12 @@ PyMethodDef* addMethods(PyMethodDef*obj1, PyMethodDef*obj2) if(obj1) for(num1=0;obj1[num1].ml_name;num1++); if(obj2) for(num2=0;obj2[num2].ml_name;num2++); PyMethodDef* result = malloc(sizeof(PyMethodDef)*(num1+num2+1)); - memcpy(result, obj1, num1*sizeof(PyMethodDef)); - memcpy(&result[num1], obj2, (num2+1)*sizeof(PyMethodDef)); - //free(obj1)? + if(obj1) + memcpy(result, obj1, num1*sizeof(PyMethodDef)); + if(obj2) + memcpy(&result[num1], obj2, (num2+1)*sizeof(PyMethodDef)); + if(obj1) + free(obj1); return result; } void setVerbosity(int _verbose) diff --git a/lib/python/tag.c b/lib/python/tag.c index afc7b11..a08e661 100644 --- a/lib/python/tag.c +++ b/lib/python/tag.c @@ -24,7 +24,10 @@ typedef struct { static void tag_dealloc(PyObject * self) { TagObject*tag = (TagObject*)self; - mylog("-%08x(%d) tag_dealoc\n", (int)self, self->ob_refcnt); + if(tag->tag) + mylog("-%08x(%d) tag_dealoc [%s]\n", (int)self, self->ob_refcnt, swf_TagGetName(tag->tag)); + else + mylog("-%08x(%d) tag_dealoc [?]\n", (int)self, self->ob_refcnt); if(tag->placeobject) { swf_PlaceObjectFree(tag->placeobject); tag->placeobject = 0; @@ -188,7 +191,7 @@ static PyObject* f_SetBackgroundColor(PyObject* self, PyObject* args, PyObject* swf_SetU8(tag->tag, rgba.r); swf_SetU8(tag->tag, rgba.g); swf_SetU8(tag->tag, rgba.b); - mylog(" %08x(%d) SetBackgroundColor(%02x,%02x,%02x)\n", (int)tag, tag->ob_refcnt, rgba.r, rgba.g, rgba.b); + mylog(" %08x(%d) SetBackgroundColor(%02x,%02x,%02x) (colorobj=%08x(%d))\n", (int)tag, tag->ob_refcnt, rgba.r, rgba.g, rgba.b, color, color->ob_refcnt); return (PyObject*)tag; } //---------------------------------------------------------------------------- @@ -203,7 +206,6 @@ static PyObject* f_DefineFont(PyObject* self, PyObject* args, PyObject* kwargs) return NULL; font = swf_LoadFont(filename); - mylog("font=%08x",font); if(!font) { PyErr_SetString(PyExc_Exception, setError("Could not load %s", filename)); return NULL; @@ -217,6 +219,12 @@ static PyObject* f_DefineFont(PyObject* self, PyObject* args, PyObject* kwargs) mylog("+%08x(%d) DefineFont\n", (int)tag, tag->ob_refcnt); return (PyObject*)tag; } +static SWFFONT* fonttag_getSWFFONT(PyObject*self) +{ + PY_ASSERT_TYPE(self, &TagClass); + TagObject*font = (TagObject*)self; + return font->font; +} //---------------------------------------------------------------------------- static PyObject* f_Protect(PyObject* self, PyObject* args, PyObject* kwargs) { @@ -247,7 +255,7 @@ static PyObject* f_DefineText(PyObject* self, PyObject* args, PyObject* kwargs) int size = 0; RGBA rgba = {255,0,0,0}; PyObject*color = 0; - TagObject*font = 0; + PyObject*font = 0; SRECT r; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!u#i|O!", kwlist, &TagClass, &font, &text, &textlen, &size, &ColorClass, &color)) @@ -264,14 +272,18 @@ static PyObject* f_DefineText(PyObject* self, PyObject* args, PyObject* kwargs) tag = (TagObject*)tag_new(); - /* notice: we do modify the (passed) font object here, - for the swf_SetDefineText call, who will write out the font id. */ - font->font->id = tagmap_add(tag->tagmap,(PyObject*)font); // add dependency on font + int font_id = tagmap_add(tag->tagmap, font); // add dependency on font + + fonttag_getSWFFONT(font)->id = font_id; tag ->tag= swf_InsertTag(0, ST_DEFINETEXT2); swf_SetU16(tag->tag, /*ID*/0); - r = swf_SetDefineText(tag->tag, font->font, &rgba, text, size); - mylog("+%08x(%d) DefineText %08x -> %08x\n", (int)tag, tag->ob_refcnt); + r = swf_SetDefineText(tag->tag, fonttag_getSWFFONT(font), &rgba, text, size); + mylog("+%08x(%d) DefineText tag=%08x \n", (int)tag, tag->ob_refcnt); + + //Py_DECREF(unicode16); + //Py_DECREF(unicode8); + //free(text); return (PyObject*)tag; } @@ -303,7 +315,7 @@ static PyObject* f_PlaceObject(PyObject* self, PyObject* args, PyObject* kwargs) &ratio, &name, &clipdepth, - &action + &ActionClass, &action )) return NULL; po->depth = depth; @@ -325,7 +337,8 @@ static PyObject* f_PlaceObject(PyObject* self, PyObject* args, PyObject* kwargs) po->id = tagmap_add(tag->tagmap,(PyObject*)character); swf_SetPlaceObject(tag->tag, po); - mylog("+%08x(%d) PlaceObject %08x\n", (int)tag, tag->ob_refcnt, character); + mylog("+%08x(%d) PlaceObject %08x(%d)\n", (int)tag, tag->ob_refcnt, character, character->ob_refcnt); + return (PyObject*)tag; } @@ -334,11 +347,11 @@ TAG* tag_getTAG(PyObject*self, TAG*prevTag, PyObject*tagmap) { TagObject*tag = (TagObject*)self; + mylog(" %08x(%d) tag_getTAG: tag=%08x id=%d (%s)", (int)self, self->ob_refcnt, tag->tag, tag->tag->id, swf_TagGetName(tag->tag)); + TAG* t = swf_InsertTag(prevTag, tag->tag->id); swf_SetBlock(t, tag->tag->data, tag->tag->len); - - mylog(" %08x(%d) tag_getTAG tagmap=%08x tag=%08x/%08x\n", (int)self, self->ob_refcnt, tagmap, tag->tag, t); - + if(swf_isDefiningTag(t)) { int newid = tagmap_add(tagmap, self); swf_SetDefineID(t, newid); @@ -353,21 +366,29 @@ TAG* tag_getTAG(PyObject*self, TAG*prevTag, PyObject*tagmap) PyObject* obj = tagmap_id2obj(tag->tagmap, id); if(obj==NULL) { PyErr_SetString(PyExc_Exception, setError("Internal error: id %d not known in taglist:")); + free(positions); return 0; } int newid = tagmap_obj2id(tagmap, obj); PUT16(&t->data[positions[i]], newid); } + free(positions); return t; } PyObject* tag_getDependencies(PyObject*self) { - mylog(" %08x(%d) tag_getDependencies\n", (int)self, self->ob_refcnt); TagObject*tag = (TagObject*)self; + mylog(" %08x(%d) tag_getDependencies id=%d tag=%s\n", (int)self, self->ob_refcnt, tag->tag->id, swf_TagGetName(tag->tag)); return tagmap_getObjectList(tag->tagmap); } +int tag_print (PyObject * self, FILE * fi, int flags) +{ + mylog(" %08x(%d) tag_print flags=%08x\n", (int)self, self->ob_refcnt, flags); + return 0; +} + PyTypeObject TagClass = { PyObject_HEAD_INIT(NULL) @@ -376,7 +397,7 @@ PyTypeObject TagClass = tp_basicsize: sizeof(TagObject), tp_itemsize: 0, tp_dealloc: tag_dealloc, - tp_print: 0, + tp_print: tag_print, tp_getattr: tag_getattr, }; static PyMethodDef TagMethods[] = diff --git a/lib/python/taglist.c b/lib/python/taglist.c index a5d28d1..8b4acf1 100644 --- a/lib/python/taglist.c +++ b/lib/python/taglist.c @@ -92,7 +92,6 @@ TAG* taglist_getTAGs(PyObject*self) } if(!firstTag) firstTag = tag; - mylog(" %08x(%d) taglist_getTAGs: added tag %08x", (int)self, self->ob_refcnt, tag); } Py_DECREF(tagmap); return firstTag; @@ -143,7 +142,7 @@ static PyObject * taglist_optimizeOrder(PyObject* self, PyObject* args) static void taglist_dealloc(PyObject* self) { TagListObject*taglist = (TagListObject*)self; - mylog("-%08x(%d) taglist_dealloc\n", (int)self, self->ob_refcnt); + mylog("-%08x(%d) taglist_dealloc list=%08x(%d)\n", (int)self, self->ob_refcnt, taglist->taglist, taglist->taglist->ob_refcnt); Py_DECREF(taglist->taglist); taglist->taglist = 0; PyObject_Del(self); @@ -191,14 +190,12 @@ static PyObject * taglist_concat(PyObject * self, PyObject* list) PyObject*tag = 0; PY_ASSERT_TYPE(self, &TagListClass); TagListObject*taglist = (TagListObject*)self; - mylog(" %08x(%d) taglist_concat %08x", (int)self, self->ob_refcnt, list); + mylog(" %08x(%d) taglist_concat %08x(%d)", (int)self, self->ob_refcnt, list, list->ob_refcnt); if (PyArg_Parse(list, "O!", &TagClass, &tag)) { - mylog(" %08x(%d) taglist_concat: Tag %08x", (int)self, self->ob_refcnt, tag); list = tag_getDependencies(tag); int l = PyList_Size(list); int t; - mylog(" %08x(%d) taglist_concat: Tag: %d dependencies", (int)self, self->ob_refcnt, l); for(t=0;tob_refcnt, tag); + mylog(" %08x(%d) taglist_concat: Adding Tag %08x(%d)", (int)self, self->ob_refcnt, tag, tag->ob_refcnt); PyList_Append(taglist->taglist, tag); + } else { + mylog(" %08x(%d) taglist_concat: Already contains Tag %08x(%d)", (int)self, self->ob_refcnt, tag, tag->ob_refcnt); } - mylog(" %08x(%d) taglist_concat: done", (int)self, self->ob_refcnt); Py_INCREF(self); return self; /* copy tag, so we don't have to do INCREF(tag) (and don't @@ -239,7 +237,6 @@ static PyObject * taglist_concat(PyObject * self, PyObject* list) if (PyList_Check(list)) { int l = PyList_Size(list); int t; - mylog(" %08x(%d) taglist_concat: List", (int)self, self->ob_refcnt); for(t=0;tob_refcnt); TagListObject*taglist2 = (TagListObject*)list; return taglist_concat(self, taglist2->taglist); @@ -295,7 +291,7 @@ static PySequenceMethods taglist_as_sequence = sq_ass_slice: 0, // x[i:j] = v intintobjargproc sq_contains: taglist_contains, //??? }; -static PyTypeObject TagListClass = +PyTypeObject TagListClass = { PyObject_HEAD_INIT(NULL) 0, diff --git a/lib/python/tagmap.c b/lib/python/tagmap.c index 317d817..0dbad9f 100644 --- a/lib/python/tagmap.c +++ b/lib/python/tagmap.c @@ -49,7 +49,6 @@ PyObject* tagmap_new() //---------------------------------------------------------------------------- int tagmap_obj2id(PyObject* self, PyObject* obj) { - mylog(" %08x(%d) tagmap_obj2id %08x", (int)self, self->ob_refcnt, obj); TagMapObject*tagmap = (TagMapObject*)self; PyObject*id = PyDict_GetItem(tagmap->obj2id, obj); if(id == 0) @@ -65,14 +64,12 @@ PyObject* tagmap_id2obj(PyObject* self, int _id) TagMapObject*tagmap = (TagMapObject*)self; PyObject*id = PyLong_FromLong(_id); PyObject*obj = PyDict_GetItem(tagmap->id2obj, id); - mylog(" %08x(%d) tagmap_id2obj %d->%08x", (int)self, self->ob_refcnt, _id, obj); Py_DECREF(id); return obj; } //---------------------------------------------------------------------------- int tagmap_getFreeID(PyObject*self) { - mylog(" %08x(%d) tagmap_getFreeID", (int)self, self->ob_refcnt); TagMapObject*tagmap = (TagMapObject*)self; int last = tagmap->currentID; do { @@ -130,8 +127,12 @@ int tagmap_add(PyObject* self, PyObject* obj) //---------------------------------------------------------------------------- void tagmap_dealloc(PyObject* self) { - mylog("-%08x(%d) tagmap_dealloc", (int)self, self->ob_refcnt); TagMapObject*tagmap = (TagMapObject*)self; + mylog("-%08x(%d) tagmap_dealloc %08x(%d) %08x(%d), %08x(%d)", (int)self, self->ob_refcnt, + tagmap->obj2id, tagmap->obj2id->ob_refcnt , + tagmap->id2obj, tagmap->id2obj->ob_refcnt , + tagmap->objlist, tagmap->objlist->ob_refcnt); + Py_DECREF(tagmap->obj2id); tagmap->obj2id = 0; Py_DECREF(tagmap->id2obj); @@ -145,7 +146,6 @@ PyObject* tagmap_getObjectList(PyObject* self) { mylog(" %08x(%d) tagmap_getObjectList", (int)self, self->ob_refcnt); TagMapObject*tagmap = (TagMapObject*)self; - Py_INCREF(tagmap->objlist); return tagmap->objlist; } //----------------------------------------------------------------------------