lava / matplotlib-cpp

Extremely simple yet powerful header-only C++ plotting library built on the popular matplotlib
MIT License
4.28k stars 1.12k forks source link

Fix some memory leaks #278

Open vdng9338 opened 2 years ago

vdng9338 commented 2 years ago

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 commented 2 years ago

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

amadeus84 commented 2 years ago

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.