Skip to content

Fix various memory leaks discovered through valgrind #3258

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 16, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 17 additions & 22 deletions lib/matplotlib/testing/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import six

import functools
import gc
import os
import sys
import shutil
Expand Down Expand Up @@ -65,20 +66,25 @@ def failer(*args, **kwargs):
return known_fail_decorator


def _do_cleanup(original_units_registry):
plt.close('all')
gc.collect()

matplotlib.tests.setup()

matplotlib.units.registry.clear()
matplotlib.units.registry.update(original_units_registry)
warnings.resetwarnings() # reset any warning filters set in tests


class CleanupTest(object):
@classmethod
def setup_class(cls):
cls.original_units_registry = matplotlib.units.registry.copy()

@classmethod
def teardown_class(cls):
plt.close('all')

matplotlib.tests.setup()

matplotlib.units.registry.clear()
matplotlib.units.registry.update(cls.original_units_registry)
warnings.resetwarnings() # reset any warning filters set in tests
_do_cleanup(cls.original_units_registry)

def test(self):
self._func()
Expand All @@ -93,13 +99,7 @@ def setUpClass(cls):

@classmethod
def tearDownClass(cls):
plt.close('all')

matplotlib.tests.setup()

matplotlib.units.registry.clear()
matplotlib.units.registry.update(cls.original_units_registry)
warnings.resetwarnings() # reset any warning filters set in tests
_do_cleanup(cls.original_units_registry)


def cleanup(func):
Expand All @@ -109,13 +109,8 @@ def wrapped_function(*args, **kwargs):
try:
func(*args, **kwargs)
finally:
plt.close('all')

matplotlib.tests.setup()
_do_cleanup(original_units_registry)

matplotlib.units.registry.clear()
matplotlib.units.registry.update(original_units_registry)
warnings.resetwarnings() #reset any warning filters set in tests
return wrapped_function


Expand Down Expand Up @@ -157,8 +152,6 @@ def test(self):
baseline_dir, result_dir = _image_directories(self._func)

for fignum, baseline in zip(plt.get_fignums(), self._baseline_images):
figure = plt.figure(fignum)

for extension in self._extensions:
will_fail = not extension in comparable_formats()
if will_fail:
Expand All @@ -182,6 +175,8 @@ def test(self):
will_fail, fail_msg,
known_exception_class=ImageComparisonFailure)
def do_test():
figure = plt.figure(fignum)

if self._remove_text:
self.remove_text(figure)

Expand Down
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);

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
4 changes: 4 additions & 0 deletions src/ft2font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -985,6 +985,10 @@ FT2Font::~FT2Font()
{
FT_Done_Glyph(glyphs[i]);
}

if (stream.descriptor.pointer != NULL) {
PyMem_Free(stream.descriptor.pointer);
}
}

int
Expand Down
18 changes: 18 additions & 0 deletions test_only.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from distutils.core import setup

import os

baseline_images = [
'baseline_images/%s/*' % x
for x in os.listdir('lib/matplotlib/tests/baseline_images')]

baseline_images += [
'mpltest.ttf',
'test_rcparams.rc'
]

setup(name='matplotlib.tests',
packages=['matplotlib.tests'],
package_dir={'matplotlib.tests': 'lib/matplotlib/tests'},
package_data={'matplotlib.tests': baseline_images}
)