Documentation

mdlTerminate

Perform any actions required at termination of the simulation

Required

Yes

Languages

C, C++

Syntax

void mdlTerminate(SimStruct *S)

Arguments

S

SimStruct representing an S-Function block.

Description

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, themdlTerminatemethod is called after a simulation (mdlStartis called).

In addition, if theSS_OPTION_CALL_TERMINATE_ON_EXIToption is set for a given S-function, and ifmdlInitializeSizesis called, then the user is guaranteed that Simulink will callmdlTerminate. One reason to set theSS_OPTION_CALL_TERMINATE_ON_EXIToption is to allocate memory inmdlInitializeSizesrather than wait untilmdlStart.

Note that Simulink callsmdlInitializeSizesunder a number of circumstances, including compilation and simulation. Simulink will also callmdlInitializeSizesduring model editing if you perform an operation such as the setting of parameters.

In C MEX S-functions, use theUNUSED_ARGmacro if themdlTerminatefunction does not perform any actions that require the SimStructSto indicate that theSinput 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.

Note

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

Thedefinestatement makes themdlTerminatemethod 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.

Example

Suppose your S-function allocates blocks of memory inmdlStartand saves pointers to the blocks in aPWorkvector. The following code fragment would free this memory.

{ int i; for (i = 0; i
           

Introduced before R2006a

Was this topic helpful?