Simulink intermittently hangs after return from mdlStart() in call to external C++ sfunction

2 views (last 30 days)
Richard Schiek
Richard Schiek on 23 Aug 2021
Answered: Mark McBroom on 8 Sep 2021
I'm writing a C++ SFunction for use with Simulink. This SFunciton connects to an exteral code to handle electrical circuit simulation.
In a typical run from Simulink, the following routines are called without any errors being reported by simulink and the C++ SFunctcion works as expecte:
mdlInitializeSizes()
mdlInitializeSampleTimes()
mdlStart()
mdlOutputs()
mdlTerminate()
But sometimes after Simulink calls mdlStart() and exits that routine it never calls mdlOutputs() and Simulink doesn't make any simulation progress. It's just stuck at time=0.
I'm not sure how to debug this as it looks like Simulink is not in my code when it hangs. The only way to stop the simulation at this point is to kill the main MATLAB job with "kill -9 <PID>".
Any suggestions on how to debug this?

Answers (1)

Mark McBroom
Mark McBroom on 8 Sep 2021
  1. Comment out or remove your S-Function block to determine if your s-function causes the hang.
  2. If it does, then you could try to use a Vis Studio debugger or gdebug to break in your mdlStart() function to take a look at what is happening. Sounds like a memory corruption issue. https://www.mathworks.com/help/simulink/sfg/debugging-c-mex-s-functions.html

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!