matplotlib-cpp icon indicating copy to clipboard operation
matplotlib-cpp copied to clipboard

Fix some memory leaks

Open vdng9338 opened this issue 4 years ago • 2 comments

In a program I wrote, I had to plot a few hundreds of matplotlib plots, and memory usage went up to 5 Gb despite calling plt::clf() and plt::close() after each plot. Removing all instances of Py_INCREF seems to solve the issue, since Py_INCREF is not needed on return values of PyObject_GetAttrString and PyObject_Call, according to https://docs.python.org/3/extending/extending.html#ownership-rules.

vdng9338 avatar Jul 16 '21 20:07 vdng9338

I didn't intend to close this PR -- looks like something weird happened.

vdng9338 avatar Aug 08 '21 16:08 vdng9338

If you're compiling with g++, compile with address sanitizer ( -fsanitize=address and link against libasan), e.g. for examples/surface.cpp

g++ -g -Wall -fsanitize=address -o surface $(python-config --includes) surface.cpp $(python-config --ldflags --embed) -lasan

This will show you all memory leaks. Before your changes, and after.

amadeus84 avatar Mar 12 '22 16:03 amadeus84