簡體   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). 然后這個 VSDLL 被一個 .exe 文件調用。 現在,我可以成功地將代碼編譯成調試和發布 DLL。 我還可以從 .exe 文件成功運行發布 dll。 它運行沒有任何問題。 但是當我嘗試在 Visual Studio 中以調試模式運行代碼時,我收到以下錯誤錯誤消息 該圖有寄存器信息和發生錯誤的堆棧幀。 堆棧幀和寄存器 還在這里附加了堆棧跟蹤信息。 堆棧跟蹤

這可能是一個愚蠢的錯誤,但我是 C++ 的新手,我對 memory 堆和堆棧沒有非常深入的了解。 我嘗試按照其他答案中的建議在調試模式下啟用/禁用不同的設置,這些答案對其他人有用,但在我的情況下沒有任何作用。 我之前使用的是 Visual Studio 專業版,我可以在沒有此錯誤的情況下進行調試。 現在最近我不得不更改為 Visual Studio 社區版,從那時起,只有當我嘗試在我的代碼中設置斷點並對其進行調試時,我才會遇到這個問題。 這可能是問題嗎? 我注意到的另一件事是,我正在使用 Visual Studio 將各種 MATLAB 函數編譯為 dll 並使用它們構建自定義 dll 以在 TRNExe 中運行。 每個都在發布模式下工作正常,但在調試模式下,每次相同的 boost_log-vc110-mt-1_49.dll 發生故障並在相同的 memory 寄存器 0x7e37a348 。 誰能幫我解決這個錯誤? 我感謝有關問題可能是什么的任何想法或建議。

請在此處查看警告消息。 這可能是問題的根源嗎? 警告信息

我在下面制作了一個最小的可重現示例。 它仍然是免費的代碼行。 另一個瓶頸是,我試圖在 Trnsys 和 MATLAB 之間進行聯合仿真。 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. 因此,C++ 將 MATLAB dll 鏈接到 TRNDll 庫,即 TRNSYS Z50484C19F21AFDAF38DZZ1A0 庫。 作為一個最小的可重現示例,我不知道如何做到這一點。 無論如何,我已經編譯了 MATLAB 代碼以添加兩個數字並在 Type201.cpp(尊重 TRNSYS 結構)中調用它並將其編譯到 DLL 以在 TRNSYS 工作室中運行。 它在那里工作。 但是,當我嘗試在調試模式下運行 Type201.cpp 時,我仍然在確切的堆棧幀和 boost_log-vc110-mt-1_49.dll 的寄存器處得到與以前完全相同的錯誤。

鍵入 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

這可能是因為某些 std 類和 boost 類在發布和調試版本中具有不同的布局。 在調試版本中,通過宏插入額外的成員,以實現更好的調試體驗,如迭代器調試等。 因此,正如 Microsoft 所記錄的,混合鏈接到不同 c++ 運行時的代碼是未定義的行為。 matlab dll 在發布模式下構建並鏈接到 c++ 發布運行時,因此它不應該被鏈接到調試庫的代碼使用!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM