User`s guide

Creating More Complex C MEX S-Functions
3-13
Note that the second argument to ssSetErrorStatus must be persistent
memory. It cannot be a local variable in your procedure. For example, the
following will cause unpredictable errors:
mdlOutputs()
{
char msg[256]; {ILLEGAL: to fix use "static char msg[256];"}
sprintf(msg,"Error due to %s", string);
ssSetErrorStatus(S,msg);
return;
}
ThessSetErrorStatus error handlingapproachis thesuggested alternativeto
using the
mexErrMsgTxt function. The function mexErrMsgTxt uses exception
handling to immediately terminate S-function execution and return control to
Simulink. In order to support exception handling inside of S-functions,
Simulink must set up exception handlers prior to each S-function invocation.
This introduces overhead into simulati on.
Exception Free Code
Youcanavoid this overhead by ensuringthat your S-function containsentirely
exception free code. Exceptionfreecodereferstocodethatneverlongjumps.
Your S-function is not exception free if it contains any routine that, when
called, has the potential of long jumping. For example
mexErrMsgTxt throwsan
exception (i.e., long jumps) when called, thus ending execution of your
S-function. Using
mxCalloc may cause unpredictable results in the event of a
memory allocation error since
mxCalloc will longjump.Ifmemory allocationis
needed, use the
stdlib.h calloc routine directly and perform your own error
handling.
If you do not call
mexErrMsgTxt or other API routines that cause exceptions,
then use the
SS_OPTION_EXCEPTION_FREE_CODE S-function option. This is done
by issuing the following command in the
mdlInitializeSizes function:
ssSetOptions(S, SS_OPTION_EXCEPTION_FREE_CODE);
Settingthis optionwillincrease theperformanceof yourS-functionby allowing
Simulink to bypass the exception handling se tup that is usually performed
prior to each S-function invocation. Extreme care must be taken to verify that
your code is exception free when using
SS_OPTION_EXCEPTION_FREE_CODE.If