Access violation while initializing matlab-compiler dll / lib in c++ only during debugging

1

what I'm trying to do is integrate a MATLAB-Compiler dll/lib to an new c++ project.

I followed this instruction: How do I integrate my C++ shared Library generated from MATLAB which seams working good (no build errors and intelisense is working good, so it seams all required information are there).

I'm using a very simple mathlab code / function for testing:

function output = extest( arg1,arg2 )
    output = arg1+arg2;
end

And the "default" c++ code for matlab functions:

#include "extest.h"
#include <cstdlib>
#include <stdio.h>

int main(int argc, char** argv){

    mclmcrInitialize();
    if (!mclInitializeApplication(NULL,0)){

        std::cerr << "could not initialize the application properly" << std::endl;
        return -1;
    }
        if(!extestInitialize()){
            std::cerr << "could not initialize the library properly" << std::endl;
            return -1;
        }
        else{
            try{

                //code itself (not jet reached therefore removed)

            }catch(const mwException& e){
              std::cerr << e.what() << std::endl;
              return -2;
            }
            catch(...){
              std::cerr << "Unexpected error thrown" << std::endl;
              return -3;  
            }
            extestTerminate();
        }
        mclTerminateApplication();
        return 0;
}

After e few moments after the debugger tries to run the line if(!extestInitialize()) the following error gets thrown.

Exception thrown at 0x000002BF72E0EE55 in DllTestingCpp.exe: 0xC0000005: Access violation reading location 0x0000000000000008.

enter image description here

I can hit visual studios continue > button and it is continued after lets say 20x click on it. Starting the code by ctrl + F5 (without debugging) everything is working good.

Any ideas why this happens in debug mode? Or better how I can get rid of this error?

PS: extest is my lib name and using Matlab R2017a 64bit and Visual Studio 2017 (debugging with x64),

c++
matlab
dllimport
matlab-compiler
lib
asked on Stack Overflow Jun 22, 2017 by user1234 • edited Jun 24, 2017 by user1234

2 Answers

1

The same problem (Matlab2017 + VS 2015) for me. Probably there is some conflict with java used by MATLAB.

I've fixed it by

const char *args[] = {"-nojvm"};
const int count = sizeof(args) / sizeof(args[0]);
mclInitializeApplication(args, count))

instead of

mclInitializeApplication(NULL,0)
answered on Stack Overflow Jan 25, 2018 by Ilya Ovodov • edited Jan 25, 2018 by Ilya Ovodov
1

I had the same issue(using VS2019) and I found the following answer here: https://uk.mathworks.com/matlabcentral/answers/182851-how-do-i-integrate-my-c-shared-library-generated-from-matlab-r2013b-in-visual-studio-2013

I encountered this same issue and reported it to Mathworks. They responded that for VS2013 and later, the debugger is set to break when 0xc0000005 occurs, even though in this case it is handled by the JVM. The fix is to go to Debug>Windows>Exception Settings>Win32 and uncheck '0xc0000005 Access Violation'. In VS2012, this setting is unchecked by default.

This seems to work o.k.

answered on Stack Overflow Jul 17, 2019 by Alexandros K • edited Jul 17, 2019 by Cris Luengo

User contributions licensed under CC BY-SA 3.0