简体   繁体   English

调试模式下的访问冲突错误,但应用程序在 Visual Studio2015 中编译的 dll 的发布模式下运行

[英]Access violation error in debug mode but application runs in release mode for a dll compiled in visual studio2015

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). 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.然后这个 VSDLL 被一个 .exe 文件调用。 Now, I can successflly compile the code into debug and release DLLs.现在,我可以成功地将代码编译成调试和发布 DLL。 I can also successfully run the release dll from the.exe file.我还可以从 .exe 文件成功运行发布 dll。 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 .但是当我尝试在 Visual Studio 中以调试模式运行代码时,我收到以下错误错误消息 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.这可能是一个愚蠢的错误,但我是 C++ 的新手,我对 memory 堆和堆栈没有非常深入的了解。 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.我之前使用的是 Visual Studio 专业版,我可以在没有此错误的情况下进行调试。 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.现在最近我不得不更改为 Visual Studio 社区版,从那时起,只有当我尝试在我的代码中设置断点并对其进行调试时,我才会遇到这个问题。 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.我注意到的另一件事是,我正在使用 Visual Studio 将各种 MATLAB 函数编译为 dll 并使用它们构建自定义 dll 以在 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.每个都在发布模式下工作正常,但在调试模式下,每次相同的 boost_log-vc110-mt-1_49.dll 发生故障并在相同的 memory 寄存器 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 和 MATLAB 之间进行联合仿真。 Trnsys has it's components written as Type DLLs (Fortran or C++) and it has a structure for this dll. Trnsys 将其组件编写为类型 DLL(Fortran 或 C++),并且它具有此 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. 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.因此,C++ 将 MATLAB dll 链接到 TRNDll 库,即 TRNSYS Z50484C19F21AFDAF38DZZ1A0 库。 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.无论如何,我已经编译了 MATLAB 代码以添加两个数字并在 Type201.cpp(尊重 TRNSYS 结构)中调用它并将其编译到 DLL 以在 TRNSYS 工作室中运行。 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.但是,当我尝试在调试模式下运行 Type201.cpp 时,我仍然在确切的堆栈帧和 boost_log-vc110-mt-1_49.dll 的寄存器处得到与以前完全相同的错误。

Type 201.cpp code is below键入 201.cpp 代码如下


    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.

```MATLAB

    function [s] = add(a,b)
         s = a+b;
    end

I complied this into dll via command line using 

    mcc -B csharedlib:addlib add.m

This is likely because some std classes and boost classes have different layout for release and debug builds.这可能是因为某些 std 类和 boost 类在发布和调试版本中具有不同的布局。 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.因此,正如 Microsoft 所记录的,混合链接到不同 c++ 运行时的代码是未定义的行为。 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! matlab dll 在发布模式下构建并链接到 c++ 发布运行时,因此它不应该被链接到调试库的代码使用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM