Skip to content

Commit 7b39e78

Browse files
committed
Fix memory leaks in the temporary rasterized images.
1 parent 760b2fc commit 7b39e78

File tree

2 files changed

+10
-16
lines changed

2 files changed

+10
-16
lines changed

src/_backend_agg.cpp

+9-15
Original file line numberDiff line numberDiff line change
@@ -1557,6 +1557,7 @@ RendererAgg::_draw_path_collection_generic
15571557

15581558
if ((Nfacecolors == 0 && Nedgecolors == 0) || Npaths == 0)
15591559
{
1560+
Py_XDECREF(transforms_arr);
15601561
return Py::Object();
15611562
}
15621563

@@ -1708,6 +1709,8 @@ RendererAgg::_draw_path_collection_generic
17081709
}
17091710
}
17101711

1712+
Py_XDECREF(transforms_arr);
1713+
17111714
return Py::Object();
17121715
}
17131716

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

23842387
int newwidth = 0;
23852388
int newheight = 0;
2386-
#if PY3K
2387-
Py::Bytes data;
2388-
#else
2389-
Py::String data;
2390-
#endif
2389+
PyObject *data;
23912390

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

2409-
#if PY3K
2410-
data = Py::Bytes(static_cast<const char*>(NULL), (int) newsize);
2411-
dst = reinterpret_cast<unsigned int*>(PyBytes_AsString(data.ptr()));
2412-
#else
2413-
data = Py::String(static_cast<const char*>(NULL), (int) newsize);
2414-
dst = reinterpret_cast<unsigned int*>(PyString_AsString(data.ptr()));
2415-
#endif
2416-
2417-
if (dst == NULL)
2408+
data = PyBytes_FromStringAndSize(NULL, newsize);
2409+
if (data == NULL)
24182410
{
24192411
throw Py::MemoryError("RendererAgg::tostring_minimized could not allocate memory");
24202412
}
2413+
dst = (unsigned int *)PyBytes_AsString(data);
24212414

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

24382431
Py::Tuple result(2);
2439-
result[0] = data;
2432+
result[0] = Py::Object(data, false);
2433+
Py_DECREF(data);
24402434
result[1] = bounds;
24412435

24422436
return result;

src/_image.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1264,7 +1264,7 @@ _image_module::frombuffer(const Py::Tuple& args)
12641264

12651265
args.verify_length(4);
12661266

1267-
PyObject *bufin = new_reference_to(args[0]);
1267+
PyObject *bufin = args[0].ptr();
12681268
size_t x = (long)Py::Int(args[1]);
12691269
size_t y = (long)Py::Int(args[2]);
12701270

0 commit comments

Comments
 (0)