Resolve Error: Code Generator Failed to Produce C++ Destructor for MATLAB Class
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 all of these conditions are true:
You generate C++ code from MATLAB code that uses classes.
You instruct the code generator to produce C++ classes from the MATLAB classes by using the parameter Generate C++ classes from MATLAB classes.
You instruct the code generator to generate reentrant code by using the Generate re-entrant code parameter.
The destructor of a class in your MATLAB code has a persistent variable or calls another function that declares and uses a persistent variable.
Possible Solutions
Depending on whether the type of code you want to generate, try one of these solutions.
Raise the Stack Limit
Increase the stack limit to generate reentrant code that has C++ classes for MATLAB classes by using one of these approaches:
In the Code Generation Settings dialog box, increase the value of the Stack usage max parameter.
In a configuration object for standalone code generation, increase the value of the
StackUsageMax
property.
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:
In the code configuration object, set the
MultiInstanceCode
property tofalse
.In the Code Generation Settings dialog box, clear the Generate re-entrant code check box.
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:
In the code configuration object, set the
CppPreserveClasses
property tofalse
.In the Code Generation Settings dialog box, clear the Generate C++ classes from MATLAB classes check box.