Perform any actions required at termination of the simulation
Yes
C, C++
void mdlTerminate(SimStruct *S)
S
SimStruct representing an S-Function block.
This method performs any actions, such as freeing of memory, that must be performed when the simulation is terminated or when an S-Function block is destroyed (e.g., when it is deleted from a model). This method is called at the end of every simulation in Fast Restart mode.
In C MEX S-functions, themdlTerminate
method is called after a simulation (mdlStart
is called).
In addition, if theSS_OPTION_CALL_TERMINATE_ON_EXIT
option is set for a given S-function, and ifmdlInitializeSizes
is called, then the user is guaranteed that Simulink will callmdlTerminate
. One reason to set theSS_OPTION_CALL_TERMINATE_ON_EXIT
option is to allocate memory inmdlInitializeSizes
rather than wait untilmdlStart
.
Note that Simulink callsmdlInitializeSizes
under a number of circumstances, including compilation and simulation. Simulink will also callmdlInitializeSizes
during model editing if you perform an operation such as the setting of parameters.
In C MEX S-functions, use theUNUSED_ARG
macro if themdlTerminate
function does not perform any actions that require the SimStructS
to indicate that theS
input argument is required, but not used in the body of the callback. To do this, insert the line
UNUSED_ARG(S)
after any declarations inmdlTerminate
.
If you have万博1manbetx®Coder™, when generating code for a noninlined C MEX S-function that contains this method, make sure the method is not wrapped in a#if defined(MATLAB_MEX_FILE)
statement. For example:
#if defined(MATLAB_MEX_FILE) static void mdlTerminate(SimStruct *S) { /* Add mdlTerminate code here * } #endif
Thedefine
statement makes themdlTerminate
method available only to a MATLAB®MEX file. If the S-function is not inlined,Simulink Codercannot use this method, resulting in link or run-time errors.
Suppose your S-function allocates blocks of memory inmdlStart
and saves pointers to the blocks in aPWork
vector. The following code fragment would free this memory.
{ int i; for (i = 0; i