Resolve Error: Code Generator Failed to Produce C++ Destructor for MATLAB Class - MATLAB & Simulink (original) (raw)

Main Content

Issue

Generating reentrant code with C++ classes from MATLAB® code increases the chances of stack overflow during code execution. Code generation might stop and produce this message:

Code generator failed to produce C++ destructor for MATLAB class 'y'. Generated code is not exception-safe. To enable generation of C++ destructor, disable 'Generate Re-entrant code (MultiInstanceCode)' configuration parameter.

This message might appear if both these conditions are true:

Possible Solutions

Depending on whether the type of code you want to generate, try one of these solutions.

Raise the Stack Limit

You can raise the stack limit to generate reentrant code that has C++ classes for MATLAB classes. Do one of the following:

Raise the stack limit by doubling the stack value until code generation succeeds, indicating that the code generator generated C++ classes in the reentrant code.

Note

The maximum configurable stack limit depends on the linker in your system. The default stack size varies based on your operating system and system configuration.

The destructor of a class in your MATLAB code must not have a persistent variable or call another function that declares a persistent variable.

Disable Reentrant Code

To generate code that contains C++ classes for MATLAB classes, you can disable generation of reentrant code. Do one of the following:

Generate Structures Instead of Classes

You can change the default behavior of the code generator to produce structures for MATLAB classes. Do one of the following: