Skip to content

Commit ca5a47b

Browse files
committed
Fix memory leaks in ttconv (that manifest themselves in the PDF and PS backends).
svn path=/trunk/matplotlib/; revision=5937
1 parent 95f4f47 commit ca5a47b

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

src/_ttconv.cpp

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,23 @@ class PythonFileWriter : public TTStreamWriter {
2525
}
2626

2727
~PythonFileWriter() {
28-
if (_write_method)
29-
Py_DECREF(_write_method);
28+
Py_XDECREF(_write_method);
3029
}
3130

3231
void set(PyObject* write_method) {
33-
if (_write_method)
34-
Py_DECREF(_write_method);
32+
Py_XDECREF(_write_method);
3533
_write_method = write_method;
36-
if (_write_method)
37-
Py_INCREF(_write_method);
34+
Py_XINCREF(_write_method);
3835
}
3936

4037
virtual void write(const char* a) {
41-
if (_write_method)
42-
if (! PyObject_CallFunction(_write_method, (char *)"s", a))
38+
PyObject* result = NULL;
39+
if (_write_method) {
40+
result = PyObject_CallFunction(_write_method, (char *)"s", a);
41+
if (! result)
4342
throw PythonExceptionOccurred();
43+
Py_DECREF(result);
44+
}
4445
}
4546
};
4647

@@ -54,6 +55,7 @@ int fileobject_to_PythonFileWriter(PyObject* object, void* address) {
5455
}
5556

5657
file_writer->set(write_method);
58+
Py_DECREF(write_method);
5759

5860
return 1;
5961
}
@@ -68,11 +70,14 @@ int pyiterable_to_vector_int(PyObject* object, void* address) {
6870
PyObject* item;
6971
while ( (item = PyIter_Next(iterator)) ) {
7072
long value = PyInt_AsLong(item);
73+
Py_DECREF(item);
7174
if (value == -1 && PyErr_Occurred())
7275
return 0;
7376
result->push_back(value);
7477
}
7578

79+
Py_DECREF(iterator);
80+
7681
return 1;
7782
}
7883

@@ -130,9 +135,13 @@ class PythonDictionaryCallback : public TTDictionaryCallback {
130135

131136
virtual void add_pair(const char* a, const char* b) {
132137
PyObject* value = PyString_FromString(b);
133-
if (value)
134-
if (PyDict_SetItemString(_dict, a, value))
138+
if (value) {
139+
if (PyDict_SetItemString(_dict, a, value)) {
140+
Py_DECREF(value);
135141
throw PythonExceptionOccurred();
142+
}
143+
}
144+
Py_DECREF(value);
136145
}
137146
};
138147

@@ -145,7 +154,7 @@ py_get_pdf_charprocs(PyObject* self, PyObject* args, PyObject* kwds) {
145154
static const char *kwlist[] = { "filename", "glyph_ids", NULL };
146155
if (! PyArg_ParseTupleAndKeywords
147156
(args, kwds,
148-
"s|O&:convert_ttf_to_ps",
157+
"s|O&:get_pdf_charprocs",
149158
(char **)kwlist,
150159
&filename,
151160
pyiterable_to_vector_int,
@@ -161,11 +170,14 @@ py_get_pdf_charprocs(PyObject* self, PyObject* args, PyObject* kwds) {
161170
try {
162171
::get_pdf_charprocs( filename, glyph_ids, dict );
163172
} catch (TTException& e) {
173+
Py_DECREF(result);
164174
PyErr_SetString(PyExc_RuntimeError, e.getMessage());
165175
return NULL;
166176
} catch (PythonExceptionOccurred& ) {
177+
Py_DECREF(result);
167178
return NULL;
168179
} catch (...) {
180+
Py_DECREF(result);
169181
PyErr_SetString(PyExc_RuntimeError, "Unknown C++ exception");
170182
return NULL;
171183
}

0 commit comments

Comments
 (0)