Recompiling with Matlab requires clear functions

Hello :wave:

I’m using the Matlab acados interface on Windows, and I’ve run into an issue when trying to recompile acados code in a single Matlab session. After I compile once, anytime I try to compile again I run into the following errors:

C:/ProgramData/MATLAB/SupportPackages/R2020b/3P.instrset/mingw_w64.instrset/bin/../lib/gcc/x86_64-w64-mingw32/6.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot open output file C:\Users\bezzo\Documents\GitHub\MPC_Occluded_Env_2\simulations\cleaned_sim\minimal_example\build\libexample.lib: Permission denied
Error using movefile
Cannot write to destination:
C:\Users\bezzo\Documents\GitHub\MPC_Occluded_Env_2\simulations\cleaned_sim\minimal_example\build\example_ocp_set_ext_fun_dyn_0_impl_dae_fun.mexw64.  Use the 'f' option to
override.

Error in ocp_set_ext_fun (line 712)
        movefile([file, '.', mexext], fullfile(opts_struct.output_dir, [mex_names{ii}, '.', mexext]));

I tried Matlab’s suggestion to use ‘f’ option in movefile, but this results in a ‘permission denied’ error at the same movefile line.

I believe the problem is that for some reason, Matlab still has files open after the first compilation that shouldn’t be kept open. Because of this, Matlab cannot overwrite the file when I try to recompile. For example, the ‘*_ocp_set_ext_fun_dyn_0_impl_dae_fun.mexw64’ file is kept open by Matlab. Below shows a screen shot of the DLL’s that are kept alive by the Matlab process, showing this file (and others).

I can’t delete the build folder, for the same reason that Matlab is still has these files open. I also tried seeing what files were still open by Matlab using the fopen(‘all’) command, but this returned nothing. I’m guessing this is because MEX has these files open, and not strictly Matlab.

Recompiling works when I quit Matlab, which (I’m guessing) force closes all files open by the Matlab process.

Here is a zip file that contains a minimal example that creates the issue on my computer.

Thanks!
Jacob

P.S. As I was typing out this question, I actually found a workaround in Matlab. The following Matlab command force closes files MEX files that are still open:

clear functions

This allows me to recompile. I decided to post anyway to raise the issue and help anyone else that might have this problem.

Hi Jacob,

How did you list those files?
Plus how exactly did you try to recompile and what (Matlab interface and/or external functions)?

One should definitely clear the acados objects before recompiling the interface.

This is why the acados Matlab examples all start with

clear all

If you have a suggestion to improve this behavior, please do so.

Best,
Jonathan

Hi Jonathan,

Thanks for the response. I list those files using Process Explorer for windows. It shows each process and what files that process has open. In my case, the files that Matlab has open are the ones that movefile is trying to change. I believe this is what causes the error.

When I say “recompile”, what I mean is re-run the Matlab script that re-creates an acados_ocp object. Deleting the acados_ocp object isn’t enough to prevent this error, since the files are still open afterwards. Maybe deleting the other acados objects (acados_ocp_model and acados_ocp_opts) is enough to close the files, like you suggest? I’ll update the post when I get a chance try.

I didn’t realize the ‘clear all’ was needed to recompile the interface, so apologies for raising the issue.

Best,
Jacob

Hi,

(and sorry for reactivating this topic.) The clear all approach is a little bit too heavy during development. With clear mex it is easier to handle and you don’t have to reinitialize all of your other variables again.

My MATLAB workflow:

clear mex
% enable these lines if no change was made from previous run
% ocp_opts.opts_struct.compile_model = 'false';
% ocp_opts.opts_struct.codgen_model = 'false';
ocp = acados_ocp(model_opts,ocp_opts);

if nothing changed set the flags to false, e.g. after restart of MATLAB and already compiled OCP. But if there was a change, probably there will be a hard MATLAB crash because of access violation.

Regards

1 Like