However, when I run my Simulink file that calls this mex-function, there is invariably a segmentation fault and Matlab crash. The error message that appears is:
-------------------------------------------------------------
This segmentation violation occurred while executing the
S-function 'controller_CMEXsfcn' in block 'Host_model/S-Function'.
A common cause of this segmentation violation is an incorrect
input port direct feedthrough setting. Each input port of the
S-function that is read (accessed) in mdlOutputs and/or
mdlGetTimeOfNextVarHit must specify that it needs its input
signal in these routines by setting direct feedthrough for
these input ports.
1) To debug your C-MEX S-function, you can enable diagnostics
by compiling the S-function source with the -g flag, e.g.,
mex -g sfunction_name.c
2) You can ask Simulink to try assuming your S-function has
direct feedthrough using:
set_param('modelname','TryForcingSFcnDF','on')
If Simulink can find a valid sorting mode that does not
result in algebraic loops involving your S-function, your
model will execute (assuming that the cause of this
segmentation violation is an incorrect direct feedthrough
setting on an input port).
See matlabroot/simulink/src/sfuntmpl_directfeed.txt
-------------------------------------------------------------
In my mdlInitializeSizes( ) function within my C code, I believe that I correctly set my 4 input ports to direct feedthrough using the code
ssSetInputPortDirectFeedThrough(S, 0, 1);
ssSetInputPortDirectFeedThrough(S, 1, 1);
ssSetInputPortDirectFeedThrough(S, 2, 1);
ssSetInputPortDirectFeedThrough(S, 3, 1);
Then, in my mdlOutputs( ) function, I refer to these 4 variables in the following way:
const real_T *var1 = (const real_T*) ssGetInputPortSignal(S,0);
const real_T *var2 = (const real_T*) ssGetInputPortSignal(S,1);
const real_T *var3 = (const real_T*) ssGetInputPortSignal(S,2);
const real_T *var4 = (const real_T*) ssGetInputPortSignal(S,3);
When I compile using the –g flag as suggested in the first portion of the error message above, the compile process completes with no comment (i.e. the >> cursor just appears, indicating no messages or errors).
When I do set_param(‘mymodelname’,’TryForcingSFcnDF’,’on’), I just get a warning message that advises me to directly set my inputs to direct feedthrough (which, shown in the code above, I believe I already do).
Mathworks Technical Support warned that my mdlUpdate( ) function is empty; however, from the documentation, I see that this function is optional, and I don’t believe that I need to update the model, for the purposes of my system.
Beyond the advice in the error message above, I’m not sure what else might be wrong with my level-2 s-function C code that eventually gets compiled into the mex file. Can anyone recognize a problem with the above code (or potentially with some other portion of my C code), that would allow the mex file to compile with no errors, but then cause a crash when the mex file is called by my Simulink file? Thanks for your input.
Currently, for debugging purposes, I am manually starting my Simulink model running. Eventually, I plan to remotely start it running via my m-file. Does it matter? Could my manual starts of the model possibly be causing this crash behavior?
I'm not the only developer, so I don't know what might be causing the issue, but here are some details: there are lots of model references in my sim, and many of the signals between model references are buses. I get a segmentation fault whether I press play or start the sim from within an m-file. The sim ran fine until I incorporated some configuration set changes given to me by someone else, and there might be other changes I missed, because it's so hard to merge mdl file changes. These changes are necessary in part to get RTW to compile the sim instead of generating code only.
Hoping you're able to resolve your own problem using the debugger.