Exception raised when trying to create a mwArray

164 Views Asked by At

I tried to make a simple C++ project to try linking it to the Matlab Runtime. Basically it looks like that :

#include <iostream>
#include "mclmcrrt.h"
#include "mclcppclass.h"
int main()
{
    std::cout << "Hello World!\n";
    mwArray D;
    std::cout << "Bye World!\n";
}

The problem is that creating the mwArray raises an exception in mclcppclass.h.

Exception raised at 0x0000000000000000 in Test Matlab Runtime.exe : 0xC0000005 : Access violation when executing at location 0x0000000000000000.

The exception location in depends on what type of data I'm trying to initalize my mwArray with. For example a double (mwArray D(1);) raises an exception here:

explicit mwArray(mxLogical re) : m_pa(0)
{
    if (mclGetScalarLogical((void**)&m_pa, re) == MCLCPP_ERR)
    mwException::raise_error();
    validate();
}

while a string (mwArray D("Test");) raises an exception here:

mwArray(const char* str) : m_pa(0)
{
    if (mclGetString((void**)&m_pa, str) == MCLCPP_ERR)
    mwException::raise_error();
    validate();
}

I just found that intializing my empty array like that mwArray D();, it generates no error (while mwArray D; generates an error).

I don't really know how to solve this issue or even where to look so if anyone can help me thank you in advance !

N.B. : A try catch what() on this code still goes in the exception. I will try Gracefully handling corrupted state exceptions to see if I can learn more about the exception.


Note: I wanted to try something else a bit unrelated but I tried the program depicted here: Create Arrays with C++ MATLAB Data API which looks like this:

matlab::data::ArrayFactory factory;
matlab::data::Array A = factory.createArray<double>({ 2,2 },
{ 1.0, 3.0, 2.0, 4.0 });

// Inspect array
matlab::data::ArrayType c = A.getType();
matlab::data::ArrayDimensions d = A.getDimensions();
size_t n = A.getNumberOfElements();

Simple program but the first line returns an exception in ArrayFactory.hpp that says:

Unhandled exception at 0x00007FFC89544F69 in Test Matlab Runtime.exe : Microsoft C++ exception : matlab::data::detail::ArrayException<matlab::Exception,11> at memory location 0x000000BF65CFF050.

The exception is raised here:

ArrayFactory() {
    typedef int (*CreateArrayFactoryFcnPtr)(typename impl::ArrayFactoryImpl**);
    static const CreateArrayFactoryFcnPtr fcn =
    detail::resolveFunction<CreateArrayFactoryFcnPtr>(
    detail::FunctionType::CREATE_ARRAY_FACTORY);

    impl::ArrayFactoryImpl* impl = nullptr;
    detail::throwIfError(fcn(&impl));       // *** Exception is raised here exactly *** //
    pImpl = std::shared_ptr<impl::ArrayFactoryImpl>(impl, [](impl::ArrayFactoryImpl* ptr) {
    typedef void (*DestroyArrayFactoryFcnPtr)(typename impl::ArrayFactoryImpl*);
    static const DestroyArrayFactoryFcnPtr destroyFcn =
    detail::resolveFunction<DestroyArrayFactoryFcnPtr>(
    detail::FunctionType::DESTROY_ARRAY_FACTORY);
    destroyFcn(ptr);
});
}

I don't know if that gives a little bit more information to solve the problem but maybe it does so I share.

N.B : A try catch what() on this code returns a Library not found.

0

There are 0 best solutions below