reworked tag memory allocation
[swftools.git] / lib / python / SWF.c
index b191927..fd7c423 100644 (file)
@@ -125,6 +125,11 @@ static PyObject* f_load(PyObject* self, PyObject* args)
     close(fi);
 
     swf->taglist = taglist_new2(swf->swf.firstTag);
+    if(swf->taglist == NULL) {
+       return NULL;
+    }
+    
+    swf_FreeTags(&swf->swf);
     swf->swf.firstTag = 0;
     
     return (PyObject*)swf;
@@ -157,14 +162,17 @@ static PyObject * swf_save(PyObject* self, PyObject* args, PyObject* kwargs)
        swf->compressed = 1;
     
     swf->firstTag = taglist_getTAGs(swfo->taglist);
+    if(!swf->firstTag)
+       return NULL;
 
     // fix the file, in case it is empty or not terminated properly
     {
        TAG*tag = swf->firstTag;
        if(!tag)
            tag = swf->firstTag = swf_InsertTag(0,ST_END);
-       while(tag && tag->next)
+       while(tag && tag->next) {
            tag = tag->next;
+       }
        if(tag->id != ST_END) {
            tag = swf_InsertTag(tag,ST_END);
        }
@@ -189,8 +197,10 @@ static PyObject * swf_save(PyObject* self, PyObject* args, PyObject* kwargs)
            }
     }
     close(fi);
-    
-    swf->firstTag = 0;
+
+    /* TODO: why is this segfaulting?? */
+   /* swf_FreeTags(swf);
+    swf->firstTag = 0;*/
     
     return PY_NONE;
 }
@@ -201,6 +211,8 @@ static PyObject * swf_writeCGI(PyObject* self, PyObject* args)
     if(!self || !PyArg_ParseTuple(args,"")) 
        return NULL;
     swf->swf.firstTag = taglist_getTAGs(swf->taglist);
+    if(!swf->swf.firstTag)
+       return NULL;
     swf_WriteCGI(&swf->swf);
     swf->swf.firstTag = 0;
     return PY_NONE;