Memory leak fixes in the wrapper #199
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Thanks to @XiDianZuoYun for bringing this to our attention through issue 198.
Getting the the
.solutionproperty of aPyOSQPSolvercalls.get_solution(), which in turn does this:This created a new PyOSQPSolution on the heap every time it was called and returned a reference to it. Since ownership was never transferred and the pointer was never stored, this leaked memory on every call.
The solution is now cached using a std::unique_ptr member variable. The object is created once on first access and automatically cleaned up in
PyOSQPSolvers destructor (which callsosqp_cleanup).Another unrelated memory leak was related to:
The actual solver is allocated elsewhere (by osqp_setup), so this allocation was unnecessary and would have leaked.
I've tested out the fixes using valgrind on a script that repeatedly access
.solution. Something like this:While this was leaking memory in the original implementation, the leak is fixed with this bug.