Skip to content

Commit 081fbe6

Browse files
authored
PR: Fix memory leaks
PR obtain from lava#278
1 parent 75c15d0 commit 081fbe6

File tree

1 file changed

+2
-19
lines changed

1 file changed

+2
-19
lines changed

include/matplotlibcpp.h

+2-19
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ struct _interpreter {
277277
s_python_function_colorbar = PyObject_GetAttrString(pymod, "colorbar");
278278
s_python_function_subplots_adjust = safe_import(pymod,"subplots_adjust");
279279
s_python_function_rcparams = PyObject_GetAttrString(pymod, "rcParams");
280-
s_python_function_spy = PyObject_GetAttrString(pymod, "spy");
280+
s_python_function_spy = PyObject_GetAttrString(pymod, "spy");
281281
#ifndef WITHOUT_NUMPY
282282
s_python_function_imshow = safe_import(pymod, "imshow");
283283
#endif
@@ -560,19 +560,16 @@ void plot_surface(const std::vector<::std::vector<Numeric>> &x,
560560

561561
PyObject *gca = PyObject_GetAttrString(fig, "gca");
562562
if (!gca) throw std::runtime_error("No gca");
563-
Py_INCREF(gca);
564563
PyObject *axis = PyObject_Call(
565564
gca, detail::_interpreter::get().s_python_empty_tuple, gca_kwargs);
566565

567566
if (!axis) throw std::runtime_error("No axis");
568-
Py_INCREF(axis);
569567

570568
Py_DECREF(gca);
571569
Py_DECREF(gca_kwargs);
572570

573571
PyObject *plot_surface = PyObject_GetAttrString(axis, "plot_surface");
574572
if (!plot_surface) throw std::runtime_error("No surface");
575-
Py_INCREF(plot_surface);
576573
PyObject *res = PyObject_Call(plot_surface, args, kwargs);
577574
if (!res) throw std::runtime_error("failed surface");
578575
Py_DECREF(plot_surface);
@@ -728,19 +725,16 @@ void plot3(const std::vector<Numeric> &x,
728725

729726
PyObject *gca = PyObject_GetAttrString(fig, "gca");
730727
if (!gca) throw std::runtime_error("No gca");
731-
Py_INCREF(gca);
732728
PyObject *axis = PyObject_Call(
733729
gca, detail::_interpreter::get().s_python_empty_tuple, gca_kwargs);
734730

735731
if (!axis) throw std::runtime_error("No axis");
736-
Py_INCREF(axis);
737732

738733
Py_DECREF(gca);
739734
Py_DECREF(gca_kwargs);
740735

741736
PyObject *plot3 = PyObject_GetAttrString(axis, "plot");
742737
if (!plot3) throw std::runtime_error("No 3D line plot");
743-
Py_INCREF(plot3);
744738
PyObject *res = PyObject_Call(plot3, args, kwargs);
745739
if (!res) throw std::runtime_error("Failed 3D line plot");
746740
Py_DECREF(plot3);
@@ -1131,19 +1125,16 @@ bool scatter(const std::vector<NumericX>& x,
11311125

11321126
PyObject *gca = PyObject_GetAttrString(fig, "gca");
11331127
if (!gca) throw std::runtime_error("No gca");
1134-
Py_INCREF(gca);
11351128
PyObject *axis = PyObject_Call(
11361129
gca, detail::_interpreter::get().s_python_empty_tuple, gca_kwargs);
11371130

11381131
if (!axis) throw std::runtime_error("No axis");
1139-
Py_INCREF(axis);
11401132

11411133
Py_DECREF(gca);
11421134
Py_DECREF(gca_kwargs);
11431135

11441136
PyObject *plot3 = PyObject_GetAttrString(axis, "scatter");
11451137
if (!plot3) throw std::runtime_error("No 3D line plot");
1146-
Py_INCREF(plot3);
11471138
PyObject *res = PyObject_Call(plot3, args, kwargs);
11481139
if (!res) throw std::runtime_error("Failed 3D line plot");
11491140
Py_DECREF(plot3);
@@ -1508,19 +1499,16 @@ bool quiver(const std::vector<NumericX>& x, const std::vector<NumericY>& y, cons
15081499

15091500
PyObject *gca = PyObject_GetAttrString(fig, "gca");
15101501
if (!gca) throw std::runtime_error("No gca");
1511-
Py_INCREF(gca);
15121502
PyObject *axis = PyObject_Call(
15131503
gca, detail::_interpreter::get().s_python_empty_tuple, gca_kwargs);
15141504

15151505
if (!axis) throw std::runtime_error("No axis");
1516-
Py_INCREF(axis);
15171506
Py_DECREF(gca);
15181507
Py_DECREF(gca_kwargs);
15191508

15201509
//plot our boys bravely, plot them strongly, plot them with a wink and clap
15211510
PyObject *plot3 = PyObject_GetAttrString(axis, "quiver");
15221511
if (!plot3) throw std::runtime_error("No 3D line plot");
1523-
Py_INCREF(plot3);
15241512
PyObject* res = PyObject_Call(
15251513
plot3, plot_args, kwargs);
15261514
if (!res) throw std::runtime_error("Failed 3D plot");
@@ -1982,11 +1970,9 @@ inline void set_aspect(Numeric ratio)
19821970
PyObject_CallObject(detail::_interpreter::get().s_python_function_gca,
19831971
detail::_interpreter::get().s_python_empty_tuple);
19841972
if (!ax) throw std::runtime_error("Call to gca() failed.");
1985-
Py_INCREF(ax);
19861973

19871974
PyObject *set_aspect = PyObject_GetAttrString(ax, "set_aspect");
19881975
if (!set_aspect) throw std::runtime_error("Attribute set_aspect not found.");
1989-
Py_INCREF(set_aspect);
19901976

19911977
PyObject *res = PyObject_Call(set_aspect, args, kwargs);
19921978
if (!res) throw std::runtime_error("Call to set_aspect() failed.");
@@ -2010,14 +1996,13 @@ inline void set_aspect_equal()
20101996
PyObject_CallObject(detail::_interpreter::get().s_python_function_gca,
20111997
detail::_interpreter::get().s_python_empty_tuple);
20121998
if (!ax) throw std::runtime_error("Call to gca() failed.");
2013-
Py_INCREF(ax);
20141999

20152000
PyObject *set_aspect = PyObject_GetAttrString(ax, "set_aspect");
20162001
if (!set_aspect) throw std::runtime_error("Attribute set_aspect not found.");
2017-
Py_INCREF(set_aspect);
20182002

20192003
PyObject *res = PyObject_Call(set_aspect, args, kwargs);
20202004
if (!res) throw std::runtime_error("Call to set_aspect() failed.");
2005+
Py_DECREF(res);
20212006
Py_DECREF(set_aspect);
20222007

20232008
Py_DECREF(ax);
@@ -2506,11 +2491,9 @@ inline void set_zlabel(const std::string &str, const std::map<std::string, std::
25062491
PyObject_CallObject(detail::_interpreter::get().s_python_function_gca,
25072492
detail::_interpreter::get().s_python_empty_tuple);
25082493
if (!ax) throw std::runtime_error("Call to gca() failed.");
2509-
Py_INCREF(ax);
25102494

25112495
PyObject *zlabel = PyObject_GetAttrString(ax, "set_zlabel");
25122496
if (!zlabel) throw std::runtime_error("Attribute set_zlabel not found.");
2513-
Py_INCREF(zlabel);
25142497

25152498
PyObject *res = PyObject_Call(zlabel, args, kwargs);
25162499
if (!res) throw std::runtime_error("Call to set_zlabel() failed.");

0 commit comments

Comments
 (0)