osqp / osqp-matlab

Matlab interface for OSQP
https://osqp.org/
Apache License 2.0
42 stars 25 forks source link

Use c_free for memory that has been allocated with c_malloc and c_calloc #34

Closed traversaro closed 2 years ago

traversaro commented 3 years ago

I was compiling the osqp_mex function against an already compiled osqp version (similar to what was described in https://github.com/oxfordcontrol/osqp-matlab/issues/27), and my MATLAB was crashing because quantities that were allocated with c_malloc and c_calloc were then freed with mxFree.

While the direct use of c_malloc and c_calloc can be converted to the corresponding mx** allocator by defining MATLAB for the osqp_mex compilation unit, this was not possible for the allocation done via csc_matrix, as fhat function is compiled as part of OSQP and whose definition can't be changed.

Changing to use c_free for freeing all the memory that was allocated with c_malloc and c_calloc fixes the problem, and works as expected even if MATLAB is defined, as in the case of the official build procedure for the MATLAB bindings.

traversaro commented 2 years ago

I updated the PR with a new commit ( https://github.com/traversaro/osqp-matlab/commit/09faf01f42f7a73fac0b03332e6b0f6976a943c8 ) as it turns out that there were other mismatched calls (c_malloc/mxFree), as reported in https://github.com/ami-iit/osqp-matlab-cmake-buildsystem/issues/5 . With this changes, I was able to run the complete test suite (without codegen tests).

imciner2 commented 2 years ago

Thanks, I don't think we had ever tried using the mex file with a version of OSQP that wasn't built for MATLAB (since we always staticly link the interface in this repo in its CMake.).