Skip to content

Commit c4e06b9

Browse files
committed
Free lp_problem_t on exception in read_mps_py
1 parent 4c4e0e1 commit c4e06b9

1 file changed

Lines changed: 3 additions & 1 deletion

File tree

python_bindings/_core_bindings.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
#include <cstdint>
1818
#include <cstring>
1919
#include <limits>
20+
#include <memory>
2021
#include <pybind11/numpy.h>
2122
#include <pybind11/pybind11.h>
2223
#include <pybind11/stl.h>
@@ -594,6 +595,8 @@ static py::dict read_mps_py(const std::string &filename)
594595
{
595596
throw std::runtime_error("Failed to read MPS file: " + filename);
596597
}
598+
// free the problem even if a conversion below throws
599+
std::unique_ptr<lp_problem_t, decltype(&lp_problem_free)> guard(prob, &lp_problem_free);
597600

598601
const int n = prob->num_variables;
599602
const int m = prob->num_constraints;
@@ -632,7 +635,6 @@ static py::dict read_mps_py(const std::string &filename)
632635
d["variable_lower_bound"] = copy_f64(prob->variable_lower_bound, n);
633636
d["variable_upper_bound"] = copy_f64(prob->variable_upper_bound, n);
634637

635-
lp_problem_free(prob);
636638
return d;
637639
}
638640

0 commit comments

Comments
 (0)