Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RTWSFCN FMU crashes on fmi2Reset() #31

Closed
dirk-runge opened this issue May 23, 2019 · 9 comments
Closed

RTWSFCN FMU crashes on fmi2Reset() #31

dirk-runge opened this issue May 23, 2019 · 9 comments
Assignees
Labels
bug Something isn't working RTWSFCN This issue is related to the S-Function based FMU export

Comments

@dirk-runge
Copy link

dirk-runge commented May 23, 2019

Hi,

I'm using FMI Toolkit version 2.4. In general it works well but I get reproducible crashes when calling fmi2Reset. Sometimes the fmi2Reset function crashes directly, sometimes the following call to fmi2DoStep crashes.

I downloaded & built today's Master-Branch (23.05.19) and compiled the FMU with that version. Still the same problem.

I'm compiling a FMI2, CoSimulation FMU on win64.

I'm working on my own Co-Simulation master and I'm not ruling out the possibility that there is a bug in my code. However, I think that's rather unlikely because FMUs generated by other tools work well.

Unfortunately my use-case requires the reset to work, so I would appreciate if you could take at looks at this. If you could explain how I can generate the .dll with debug information included, I might be able to track down the problem for you.

Best Regards
Dirk

@t-sommer
Copy link
Contributor

I'm using FMI Toolkit version 2.4. In general it works well but I get reproducible crashes when calling fmi2Reset. Sometimes the fmi2Reset function crashes directly, sometimes the following call to fmi2DoStep crashes.

Can you provide steps to reproduce the problem (MATLAB version, exported model, solver settings, target)?

I downloaded & built today's Master-Branch (23.05.19) and compiled the FMU with that version. Still the same problem.

To get the latest development version you should use the develop branch. Master gets only updated on releases.

If you could explain how I can generate the .dll with debug information included, I might be able to track down the problem for you.

Creating a debug version is easy. grtfmi.tlc creates a Visual Studio solution in the build folder (modelname_grt_fmi_rtw). Building the project in debug mode yields an FMU with a debug DLL in the build folder.
For the rtwsfcnfmi.tlc target you can use the included rtwsfcnfmi/CMakeLists.txt to create a Visual Studio solution from the generated source code.

@dirk-runge
Copy link
Author

dirk-runge commented May 24, 2019

Thanks for your answer Torsten.

I was able to build a debug-DLL by generating a Visual Studio solution with grtfmi.tlc and then building the debug configuration. However, the debug-DLL does not support the fmi2Reset function at all. If I step into the fmi2Reset call with the Visual Studio debugger I end up in fmi2Functions.c line 89, where a constant fmi2Error is returned.

The rtwsfcnfmi.tlc seems to use the fmi2Reset implementation in fmi2.c line 462 instead. My rtwsfcnfmi.tlc-generated FMU still crashes with the development-version. I didn't fully understand how I can build a debug-version for rtwsfcnfmi.tlc. Are you saying it's enough to build the Debug version instead of the Release version (i.e. changing DCMAKE_BUILD_TYPE=Release to DCMAKE_BUILD_TYPE=Debug in build.bat)? I could do that but isn't the build-process in Matlab still using compiler optimization flags?

We exported many different models as FMUs that use different solvers, step-sizes etc. They all crash. However, I created a "minimal test" model that uses a fixed step ode3 solver with step size 1e-3. We use both R2015b and R2017b to generate FMUs (both crash).

Usually it takes multiple calls to fmi2Reset before the crash happens. Yesterday it always took exactly 2037 calls to fmi2Reset until the crash happened. In the end it crashes because it de-references a NULL-Pointer. Of course it's hard to predict what will happen if there is a bad pointer...

As I said in general the FMU works well. I run through the usual initialization stuff like fmi2SetupExperiment, fmi2EnterInitalizationMode, fmi2ExitInitializationMode. After that it just does a Loop of fmi2SetXXX for the inputs, fmi2DoStep, fmi2GetXXX for the outputs. Now, when my application wants to reset the model, it calls fmi2Reset and then runs through the initialization again (fmi2SetupExperiment....) and it will then crash on the call to fmi2DoStep.

@dirk-runge
Copy link
Author

dirk-runge commented May 24, 2019

I found out that the crash is related to the calls to
rt_DestroyIntegrationData(model->S);
rt_CreateIntegrationData(model->S);
in fmi2Reset.

If I comment these two lines it doesn't crash anymore.

@t-sommer t-sommer changed the title Crash on fmi2Reset RTWSFCN FMU crashes on fmi2Reset() May 24, 2019
@t-sommer t-sommer added bug Something isn't working export RTWSFCN This issue is related to the S-Function based FMU export and removed export labels Jun 26, 2019
@APillekeit
Copy link

I made the same observation: FMU exported with grtfmi.tlc and FMI Kit 2.6 crashes at fmi2Reset() due to illegal memory access.

@t-sommer
Copy link
Contributor

@APillekeit, can you reproduce the problem with the latest version grtfmi.tlc on develop?

patengel added a commit to patengel/FMIKit-Simulink that referenced this issue Aug 18, 2020
@patengel
Copy link

I came across the same issue and have a proposal how to fix it.
It would be nice if you have a short look at the proposal.

@t-sommer
Copy link
Contributor

t-sommer commented Aug 25, 2020

It would be nice if you have a short look at the proposal.

@patengel, please open a new issue and provide steps to reproduce the problem with the latest release.

@APillekeit
Copy link

Hi,

I'm sorry I cannot look into this because I don't have access to Simulink anymore.

My old issue was already solved and the code was integrated in the new release. Thanks again for that!

My recommendation for a workaround would be to avoid the use of fmi2Reset() - if possible - and to reinstantiate the model via fmi2FreeInstance() and fmi2Instantiate(). In my oppinion this is safer anyhow because it also safely resets hidden variables/states of badly written code inside of the FMU model (e.g. in s-functions).

@t-sommer
Copy link
Contributor

t-sommer commented Sep 16, 2020

@Krid82, @APillekeit, can you give d2acf28 497993f a try?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working RTWSFCN This issue is related to the S-Function based FMU export
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants