Skip to content

Commit

Permalink
Fix memory leaks in the temporary rasterized images.
Browse files Browse the repository at this point in the history
  • Loading branch information
mdboom committed Jul 14, 2014
1 parent 760b2fc commit 7b39e78
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 16 deletions.
24 changes: 9 additions & 15 deletions src/_backend_agg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1557,6 +1557,7 @@ RendererAgg::_draw_path_collection_generic

if ((Nfacecolors == 0 && Nedgecolors == 0) || Npaths == 0)
{
Py_XDECREF(transforms_arr);
return Py::Object();
}

Expand Down Expand Up @@ -1708,6 +1709,8 @@ RendererAgg::_draw_path_collection_generic
}
}

Py_XDECREF(transforms_arr);

return Py::Object();
}

Expand Down Expand Up @@ -2383,11 +2386,7 @@ RendererAgg::tostring_rgba_minimized(const Py::Tuple& args)

int newwidth = 0;
int newheight = 0;
#if PY3K
Py::Bytes data;
#else
Py::String data;
#endif
PyObject *data;

if (xmin < xmax && ymin < ymax)
{
Expand All @@ -2406,18 +2405,12 @@ RendererAgg::tostring_rgba_minimized(const Py::Tuple& args)
// the _AsString() API.
unsigned int* dst;

#if PY3K
data = Py::Bytes(static_cast<const char*>(NULL), (int) newsize);
dst = reinterpret_cast<unsigned int*>(PyBytes_AsString(data.ptr()));
#else
data = Py::String(static_cast<const char*>(NULL), (int) newsize);
dst = reinterpret_cast<unsigned int*>(PyString_AsString(data.ptr()));
#endif

if (dst == NULL)
data = PyBytes_FromStringAndSize(NULL, newsize);
if (data == NULL)
{
throw Py::MemoryError("RendererAgg::tostring_minimized could not allocate memory");
}
dst = (unsigned int *)PyBytes_AsString(data);

This comment has been minimized.

Copy link
@WeatherGod

WeatherGod Jul 25, 2014

Member

This is subtly different from the original code. In 2.x, this would be PyString_AsString(), now it is always PyBytes_AsString() (which would match with PyBytes_FromStringAndSize(). However, the data pointer is now a PyObject pointer, not a Py::Bytes pointer. Could this cause issues?

This comment has been minimized.

Copy link
@mdboom

mdboom Jul 25, 2014

Author Member

On Python 2, PyBytes_AsString() is an alias for PyString_AsString(), so they are the same. I think PyBytes is more explicit and clear, and also doesn't require separate code paths for Python 2 and 3.


unsigned int* src = (unsigned int*)pixBuffer;
for (int y = ymin; y < ymax; ++y)
Expand All @@ -2436,7 +2429,8 @@ RendererAgg::tostring_rgba_minimized(const Py::Tuple& args)
bounds[3] = Py::Int(newheight);

Py::Tuple result(2);
result[0] = data;
result[0] = Py::Object(data, false);
Py_DECREF(data);
result[1] = bounds;

return result;
Expand Down
2 changes: 1 addition & 1 deletion src/_image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1264,7 +1264,7 @@ _image_module::frombuffer(const Py::Tuple& args)

args.verify_length(4);

PyObject *bufin = new_reference_to(args[0]);
PyObject *bufin = args[0].ptr();
size_t x = (long)Py::Int(args[1]);
size_t y = (long)Py::Int(args[2]);

Expand Down

0 comments on commit 7b39e78

Please sign in to comment.