@@ -2261,17 +2261,21 @@ RendererAgg::write_rgba(const Py::Tuple& args) {
22612261static void write_png_data (png_structp png_ptr, png_bytep data, png_size_t length) {
22622262 PyObject* py_file_obj = (PyObject*)png_get_io_ptr (png_ptr);
22632263 PyObject* write_method = PyObject_GetAttrString (py_file_obj, " write" );
2264- PyObject_CallFunction (write_method, " s#" , data, length);
2265-
2266- // MGDTODO: Check NULL on failure
2264+ PyObject* result = NULL ;
2265+ if (write_method)
2266+ result = PyObject_CallFunction (write_method, " s#" , data, length);
2267+ Py_XDECREF (write_method);
2268+ Py_XDECREF (result);
22672269}
22682270
22692271static void flush_png_data (png_structp png_ptr) {
22702272 PyObject* py_file_obj = (PyObject*)png_get_io_ptr (png_ptr);
22712273 PyObject* flush_method = PyObject_GetAttrString (py_file_obj, " flush" );
2272- if (flush_method) {
2273- PyObject_CallFunction (flush_method, " " );
2274- }
2274+ PyObject* result = NULL ;
2275+ if (flush_method)
2276+ result = PyObject_CallFunction (flush_method, " " );
2277+ Py_XDECREF (flush_method);
2278+ Py_XDECREF (result);
22752279}
22762280
22772281// this code is heavily adapted from the paint license, which is in
@@ -2294,8 +2298,11 @@ RendererAgg::write_png(const Py::Tuple& args)
22942298 }
22952299 else {
22962300 PyObject* write_method = PyObject_GetAttrString (py_fileobj.ptr (), " write" );
2297- if (!(write_method && PyCallable_Check (write_method)))
2301+ if (!(write_method && PyCallable_Check (write_method))) {
2302+ Py_XDECREF (write_method);
22982303 throw Py::TypeError (" Object does not appear to be a path or a Python file-like object" );
2304+ }
2305+ Py_XDECREF (write_method);
22992306 }
23002307
23012308 png_bytep *row_pointers = NULL ;
0 commit comments