Revision: 5945
          http://matplotlib.svn.sourceforge.net/matplotlib/?rev=5945&view=rev
Author:   mdboom
Date:     2008-08-01 15:00:35 +0000 (Fri, 01 Aug 2008)

Log Message:
-----------
Backport memory leak fixes in _ttconv.cpp

Modified Paths:
--------------
    branches/v0_91_maint/CHANGELOG
    branches/v0_91_maint/src/_ttconv.cpp

Modified: branches/v0_91_maint/CHANGELOG
===================================================================
--- branches/v0_91_maint/CHANGELOG      2008-08-01 12:19:26 UTC (rev 5944)
+++ branches/v0_91_maint/CHANGELOG      2008-08-01 15:00:35 UTC (rev 5945)
@@ -1,3 +1,5 @@
+2008-08-01 Backported memory leak fixes in _ttconv.cpp - MGD
+
 2008-07-24 Deprecated (raise NotImplementedError) all the mlab2
            functions from matplotlib.mlab out of concern that some of
            them were not clean room implementations. JDH
@@ -2,3 +4,3 @@
 
-2008-07-16 Improve error handling in texmanager, thanks to Ian Henry 
+2008-07-16 Improve error handling in texmanager, thanks to Ian Henry
            for reporting - DSD

Modified: branches/v0_91_maint/src/_ttconv.cpp
===================================================================
--- branches/v0_91_maint/src/_ttconv.cpp        2008-08-01 12:19:26 UTC (rev 
5944)
+++ branches/v0_91_maint/src/_ttconv.cpp        2008-08-01 15:00:35 UTC (rev 
5945)
@@ -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;
 }
 
@@ -83,11 +88,11 @@
   int                  fonttype;
   std::vector<int>     glyph_ids;
 
-  static const char *kwlist[] = { 
+  static const char *kwlist[] = {
     "filename", "output", "fonttype", "glyph_ids", NULL };
   if (! PyArg_ParseTupleAndKeywords
-      (args, kwds, 
-       "sO&i|O&:convert_ttf_to_ps", 
+      (args, kwds,
+       "sO&i|O&:convert_ttf_to_ps",
        (char**)kwlist,
        &filename,
        fileobject_to_PythonFileWriter,
@@ -96,9 +101,9 @@
        pyiterable_to_vector_int,
        &glyph_ids))
     return NULL;
-  
+
   if (fonttype != 3 && fonttype != 42) {
-    PyErr_SetString(PyExc_ValueError, 
+    PyErr_SetString(PyExc_ValueError,
                    "fonttype must be either 3 (raw Postscript) or 42 "
                    "(embedded Truetype)");
     return NULL;
@@ -109,7 +114,7 @@
   } catch (TTException& e) {
     PyErr_SetString(PyExc_RuntimeError, e.getMessage());
     return NULL;
-  } catch (PythonExceptionOccurred& e) {
+  } catch (PythonExceptionOccurred& ) {
     return NULL;
   } catch (...) {
     PyErr_SetString(PyExc_RuntimeError, "Unknown C++ exception");
@@ -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);
   }
 };
 
@@ -144,8 +153,8 @@
 
   static const char *kwlist[] = { "filename", "glyph_ids", NULL };
   if (! PyArg_ParseTupleAndKeywords
-      (args, kwds, 
-       "s|O&:convert_ttf_to_ps", 
+      (args, kwds,
+       "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& e) {
+  } catch (PythonExceptionOccurred& ) {
+    Py_DECREF(result);
     return NULL;
   } catch (...) {
+    Py_DECREF(result);
     PyErr_SetString(PyExc_RuntimeError, "Unknown C++ exception");
     return NULL;
   }
@@ -174,7 +186,7 @@
 }
 
 static PyMethodDef ttconv_methods[] = {
-  {"convert_ttf_to_ps", (PyCFunction)convert_ttf_to_ps, METH_KEYWORDS, 
+  {"convert_ttf_to_ps", (PyCFunction)convert_ttf_to_ps, METH_KEYWORDS,
    "convert_ttf_to_ps(filename, output, fonttype, glyph_ids)\n"
    "\n"
    "Converts the Truetype font into a Type 3 or Type 42 Postscript font, "
@@ -191,7 +203,7 @@
    "then all glyphs will be included.  If any of the glyphs specified are "
    "composite glyphs, then the component glyphs will also be included."
   },
-  {"get_pdf_charprocs", (PyCFunction)py_get_pdf_charprocs, METH_KEYWORDS, 
+  {"get_pdf_charprocs", (PyCFunction)py_get_pdf_charprocs, METH_KEYWORDS,
    "get_pdf_charprocs(filename, glyph_ids)\n"
    "\n"
    "Given a Truetype font file, returns a dictionary containing the PDF Type 
3\n"
@@ -204,14 +216,14 @@
    "the values are the stream content needed to render that glyph.  This\n"
    "is useful to generate the CharProcs dictionary in a PDF Type 3 font.\n"
   },
-  {NULL}  /* Sentinel */
+  {0, 0, 0, 0}  /* Sentinel */
 };
 
 #ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
 #define PyMODINIT_FUNC void
 #endif
 PyMODINIT_FUNC
-initttconv(void) 
+initttconv(void)
 {
     PyObject* m;
 


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