Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 490fa9c

Browse files
alexdewarlava
authored andcommitted
Fix memory leaks in xlim() and ylim()
Memory is allocated with new and delete is never called. Use a std::array instead, so no memory will be allocated.
1 parent 3af24b4 commit 490fa9c

File tree

1 file changed

+10
-20
lines changed

1 file changed

+10
-20
lines changed

matplotlibcpp.h

+10-20
Original file line numberDiff line numberDiff line change
@@ -1896,43 +1896,33 @@ void xlim(Numeric left, Numeric right)
18961896
}
18971897

18981898

1899-
inline double* xlim()
1899+
inline std::array<double, 2> xlim()
19001900
{
1901-
detail::_interpreter::get();
1902-
19031901
PyObject* args = PyTuple_New(0);
19041902
PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_xlim, args);
1905-
PyObject* left = PyTuple_GetItem(res,0);
1906-
PyObject* right = PyTuple_GetItem(res,1);
1907-
1908-
double* arr = new double[2];
1909-
arr[0] = PyFloat_AsDouble(left);
1910-
arr[1] = PyFloat_AsDouble(right);
19111903

19121904
if(!res) throw std::runtime_error("Call to xlim() failed.");
19131905

19141906
Py_DECREF(res);
1915-
return arr;
1907+
1908+
PyObject* left = PyTuple_GetItem(res,0);
1909+
PyObject* right = PyTuple_GetItem(res,1);
1910+
return { PyFloat_AsDouble(left), PyFloat_AsDouble(right) };
19161911
}
19171912

19181913

1919-
inline double* ylim()
1914+
inline std::array<double, 2> ylim()
19201915
{
1921-
detail::_interpreter::get();
1922-
19231916
PyObject* args = PyTuple_New(0);
19241917
PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_ylim, args);
1925-
PyObject* left = PyTuple_GetItem(res,0);
1926-
PyObject* right = PyTuple_GetItem(res,1);
1927-
1928-
double* arr = new double[2];
1929-
arr[0] = PyFloat_AsDouble(left);
1930-
arr[1] = PyFloat_AsDouble(right);
19311918

19321919
if(!res) throw std::runtime_error("Call to ylim() failed.");
19331920

19341921
Py_DECREF(res);
1935-
return arr;
1922+
1923+
PyObject* left = PyTuple_GetItem(res,0);
1924+
PyObject* right = PyTuple_GetItem(res,1);
1925+
return { PyFloat_AsDouble(left), PyFloat_AsDouble(right) };
19361926
}
19371927

19381928
template<typename Numeric>

0 commit comments

Comments
 (0)