opensim-core icon indicating copy to clipboard operation
opensim-core copied to clipboard

Interacting with a Model object after it was used in a function crashes MATLAB

Open VDB-Bram opened this issue 2 years ago • 2 comments

I am working with the OpenSim API in Matlab R2021b on a Windows 10 machine. For some days already I am experiencing some problems when adjusting my model through the API.

I am working on a MATLAB script to go from a generic osim model to a personlized one (joint locations and functions, muscle path points, muscle tendon parameters, marker locations). For this I created several functions using the API. I pass the Model and some variables to the function and ask the updated Model back as an output.

One of the problems that I cannot get fixed is the following. I've made a function to convert the simmsplines in the knee to a personalized polynomial function. I call this function two times, once for every knee. The function seems to do what it supposed to do. But, problems arrise when I try to print the updated model. More precisely, if I let the script run past .print() the model is written to a file without any problem, but when I put a breakpoint at the line where the model is printed and then run this line afterwards, MATLAB crashes. See MATLAB crash log file.txt. The same happens when I try to interact with the same model after is was written away succesfully. I have already tried multiple combinations of using .finalizeConnections, .updJointSet, .updModel, .initSystem etc. in the function as well as in the main script, without any succes.

I think this problem might be related to #3204, but I am not sure. Any help with this issue is appreciated.

If additional information is needed, please let me know.

VDB-Bram avatar May 24 '22 09:05 VDB-Bram

This likely is related to memory management where the Function goes out of scope the spline gets deleted and your code tries to access it after it was garbage collected. You maybe able to pass in a clone of the spline but it's hard to tell the specifics without a code snippet and possibly looking into the C++ code to inspect the specific interface.

aymanhab avatar May 25 '22 17:05 aymanhab

Dear Ayman

Attached to this message are the scripts. Running the main script up to line 176 and continuing after 10 seconds is a good way to induce the error/crash.

Any advice on what to change to solve this problem is much appreciated. I myself am not too familiar with C++ code. I use this code with OpenSim 4.3 API.

MStoOsim_os4_v2.zip

VDB-Bram avatar Jun 14 '22 07:06 VDB-Bram

This boils down to a function 'setFunction()' taking ownership of a passed in function pointer without the runtime knowing about it, causing stale pointer and crash after the function goes out of scope. Possible solutions (cloneFunction that doesn't take owenership, or switch to shared_ptr). Investigating further..

aymanhab avatar Dec 16 '22 19:12 aymanhab

We believe this issue is resolved; please feel free to add a comment if issues remain.

jenhicks avatar Jan 17 '23 19:01 jenhicks