Reputation: 11
I am using visual studio 2015 in 32bit mode to compile a dll(VSDll) which calls the functions written in another dll (MATLAB function exported as dll by MATLAB C++ compiler). This VSDLL is then called by an .exe file. Now, I can successflly compile the code into debug and release DLLs. I can also successfully run the release dll from the .exe file. It runs without any problem. But when I try to run the code in debug mode in visual studio I get the following error Error message. This figure has the register information and the stack frame where the error occurs.Stack Frame and registers. Also attached the stack trace information here. Stack trace.
This might be a silly error but I am new to C++ and I don't have a very deep understanding of memory heaps and stacks. I tried enabling/disabling different settings in the debug mode as suggested in other answers which have worked for others but nothing works in my case. I was using the Visual studio professional version before and I could debug without this error. Now recently I had to change to visual studio community edition and since then I have this problem only when I try to set breakpoints in my code and debug it. Could this be the problem? Another thing I have noticed is, I am using Visual studio to compile various MATLAB functions as dll and use them to build customized dlls to run in TRNExe. Each of it works fine in the release mode, but in debug mode, everytime the same boost_log-vc110-mt-1_49.dll that breaks down and at the same memory register 0x7e37a348. Can anyone please help me solving this error? I appreciate any ideas or suggestions regarding what the problem could be.
Please see the warning messages here. Could this be the source of the problem?Warning message
I have produced a minimal reproducible example below. It's still free lines of code. Another bottleneck for this is, I am trying to co-simulate between Trnsys and MATLAB. Trnsys has it's components written as Type DLLs (Fortran or C++) and it has a structure for this dll. I am trying to export the MATLAB function as dll, call it in the Trnsys type at required parts of the code and compile it as the DLL to run in Trnsys simulation studio. So, C++ links the MATLAB dll to the TRNDll which is the TRNSYS kernel library. I don't know how to do that as a minimal reproducible example. Anyways, I have compiled the MATLAB code to add two numbers and call it in the Type201.cpp(Respecting the TRNSYS structure) and compiled it into the DLL to run in TRNSYS studio. It works there. But when I try to run the Type201.cpp in debug mode, I still get the exact same error as before at the exact stack frame and the register with boost_log-vc110-mt-1_49.dll.
Type 201.cpp code is below
extern "C" __declspec(dllexport) void TYPE201(void)
{
double a;
double b;
double Timestep, Time, StartTime, StopTime;
int index, CurrentUnit, CurrentType;
mxArray* x_ptr ;
mxArray* y_ptr ;
mxArray* z_ptr = NULL;
double* output = NULL;
//Get the Global Trnsys Simulation Variables
Time = getSimulationTime();
Timestep = getSimulationTimeStep();
CurrentUnit = getCurrentUnit();
CurrentType = getCurrentType();
StartTime = getSimulationStartTime();
StopTime = getSimulationStopTime();
//Set the Version Number for This Type
if (getIsVersionSigningTime())
{
int v = 17;
setTypeVersion(&v);
return;
}
//Do All of the Last Call Manipulations Here
if (getIsLastCallofSimulation())
{
addlibTerminate();
mclTerminateApplication();
return;
}
//Perform Any "End of Timestep" Manipulations That May Be Required
//Do All of the "Very First Call of the Simulation Manipulations" Here
if (getIsFirstCallofSimulation())
{
//Tell the TRNSYS Engine How This Type Works
int npar = 2;
int nin = 2;
int nder = 0;
int nout = 1;
int mode = 1;
int staticStore = 0;
int dynamicStore =1;
setNumberofParameters(&npar);
setNumberofInputs(&nin);
setNumberofDerivatives(&nder);
setNumberofOutputs(&nout);
setIterationMode(&mode);
setNumberStoredVariables(&staticStore, &dynamicStore);
return;
}
//Do All of the "Start Time" Manipulations Here - There Are No Iterations at the Intial Time
if (getIsStartTime())
{
index = 1; a = getInputValue(&index);
index = 2; b = getInputValue(&index);
if (!mclInitializeApplication(NULL, 0))
{
//fprintf(stderr, "Could not initialize the application.\n");
exit(1);
}
if (!addlibInitialize())
{
//fprintf(stderr, "Could not initialize the library.\n");
exit(1);
}
//Read in the Values of the Inputs from the Input File
return;
}
if (getIsEndOfTimestep())
{
return;
}
//---------------------------------------------------------------------------------------------------------------------- -
//ReRead the Parameters if Another Unit of This Type Has Been Called Last
//Get the Current Inputs to the Model
index = 1;
a = getInputValue(&index);
index = 2;
b = getInputValue(&index);
int noutput = getNumberOfOutputs();
//Create an mxArray to input into mlfAdd
x_ptr = mxCreateDoubleMatrix(1, 1, mxREAL);
y_ptr = mxCreateDoubleMatrix(1, 1, mxREAL);
memcpy(mxGetPr(x_ptr), &a, 1 * sizeof(double));
memcpy(mxGetPr(y_ptr), &b, 1 * sizeof(double));
mlfAdd(1, &z_ptr, y_ptr, x_ptr);
output = mxGetPr(z_ptr);
index = 1;
setOutputValue(&index, output);
return;
}
Then this is the MATLAB function to add two numbers.
function [s] = add(a,b)
s = a+b;
end
I complied this into dll via command line using
mcc -B csharedlib:addlib add.m
Upvotes: 1
Views: 1436
Reputation: 1605
This is likely because some std classes and boost classes have different layout for release and debug builds. In debug builds additional members are inserted via macros to enable better debugging experience like iterator debugging and such. So as documented by Microsoft it is undefined behavior to mix code linked to different c++ runtime. The matlab dll is build in release mode and linked to c++ release runtime so it shouldn't be used by code linked to debug libraries!
Upvotes: 4