stephenbeckr / L-BFGS-B-C

L-BFGS-B, converted from Fortran to C, with Matlab wrapper
BSD 3-Clause "New" or "Revised" License
109 stars 57 forks source link

Memory leak in mex wrapper #12

Closed jml70 closed 3 months ago

jml70 commented 3 months ago

There is a memory leak in lbfgsb_wrapper.c which results in memory allocated within the call to the objective function and its gradient not being freed. As a result, the solver allocates an additional chunk of memory (equal to the size of the gradient) every iteration.

The fix is fairly simple, move the calls to mxDestroyArray( LHS[...] ) on lines 324-325 inside the loop to line 313.

stephenbeckr commented 3 months ago

Great catch! Can you make the changes into a pull request?

stephenbeckr commented 3 months ago

Ok, I made the change, but I've only recompiled the mac intel version. If anyone wants to contribute compiled versions for windows, linux and Apple silicon, I'd gladly incorporate those and make a new release