Revision: 5937
          http://matplotlib.svn.sourceforge.net/matplotlib/?rev=5937&view=rev
Author:   mdboom
Date:     2008-07-31 15:37:56 +0000 (Thu, 31 Jul 2008)

Log Message:
-----------
Fix memory leaks in ttconv (that manifest themselves in the PDF and PS 
backends).

Modified Paths:
--------------
    trunk/matplotlib/src/_ttconv.cpp

Modified: trunk/matplotlib/src/_ttconv.cpp
===================================================================
--- trunk/matplotlib/src/_ttconv.cpp    2008-07-31 14:44:07 UTC (rev 5936)
+++ trunk/matplotlib/src/_ttconv.cpp    2008-07-31 15:37:56 UTC (rev 5937)
@@ -25,22 +25,23 @@
   }
 
   ~PythonFileWriter() {
-    if (_write_method)
-      Py_DECREF(_write_method);
+    Py_XDECREF(_write_method);
   }
 
   void set(PyObject* write_method) {
-    if (_write_method)
-      Py_DECREF(_write_method);
+    Py_XDECREF(_write_method);
     _write_method = write_method;
-    if (_write_method)
-      Py_INCREF(_write_method);
+    Py_XINCREF(_write_method);
   }
 
   virtual void write(const char* a) {
-    if (_write_method)
-      if (! PyObject_CallFunction(_write_method, (char *)"s", a))
+    PyObject* result = NULL;
+    if (_write_method) {
+      result = PyObject_CallFunction(_write_method, (char *)"s", a);
+      if (! result)
        throw PythonExceptionOccurred();
+      Py_DECREF(result);
+    }
   }
 };
 
@@ -54,6 +55,7 @@
   }
 
   file_writer->set(write_method);
+  Py_DECREF(write_method);
 
   return 1;
 }
@@ -68,11 +70,14 @@
   PyObject* item;
   while ( (item = PyIter_Next(iterator)) ) {
     long value = PyInt_AsLong(item);
+    Py_DECREF(item);
     if (value == -1 && PyErr_Occurred())
       return 0;
     result->push_back(value);
   }
 
+  Py_DECREF(iterator);
+
   return 1;
 }
 
@@ -130,9 +135,13 @@
 
   virtual void add_pair(const char* a, const char* b) {
     PyObject* value = PyString_FromString(b);
-    if (value)
-      if (PyDict_SetItemString(_dict, a, value))
+    if (value) {
+      if (PyDict_SetItemString(_dict, a, value)) {
+        Py_DECREF(value);
        throw PythonExceptionOccurred();
+      }
+    }
+    Py_DECREF(value);
   }
 };
 
@@ -145,7 +154,7 @@
   static const char *kwlist[] = { "filename", "glyph_ids", NULL };
   if (! PyArg_ParseTupleAndKeywords
       (args, kwds,
-       "s|O&:convert_ttf_to_ps",
+       "s|O&:get_pdf_charprocs",
        (char **)kwlist,
        &filename,
        pyiterable_to_vector_int,
@@ -161,11 +170,14 @@
   try {
     ::get_pdf_charprocs( filename, glyph_ids, dict );
   } catch (TTException& e) {
+    Py_DECREF(result);
     PyErr_SetString(PyExc_RuntimeError, e.getMessage());
     return NULL;
   } catch (PythonExceptionOccurred& ) {
+    Py_DECREF(result);
     return NULL;
   } catch (...) {
+    Py_DECREF(result);
     PyErr_SetString(PyExc_RuntimeError, "Unknown C++ exception");
     return NULL;
   }


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Matplotlib-checkins mailing list
Matplotlib-checkins@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-checkins

Reply via email to