1

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.

Jack
  • 695
  • 10
  • 29
  • 1
    `mwArray D();` is a function declaration, the correct way would be `mwArray D{};` but it is unrelated to your problem. – Quimby Aug 04 '22 at 09:29
  • @Quimby ah yes indeed! – Jack Aug 04 '22 at 09:30
  • It might be a good idea to show how exactly you initialize the array, the exception comes from dereferencing a null pointer. – Quimby Aug 04 '22 at 09:41
  • 1
    @Quimby I initialize it the way I showed in the first code in my post. To initalize it empty I use `mwArray D;`, for a int/double `mwArray D(1);`, for a string `mwArray D("Test");`. And each raise an exception (where `mclcppclass.h` tests the type of data). (I added these examples in the original post) – Jack Aug 04 '22 at 09:48
  • For the unhandled exception: catch the exception in your code, and print its `what()` string, you might learn something more about the issue. If that doesn’t work, build your program using the sanitizer, or run it under the debugger, and keep poking until you figure out what is happening! – Cris Luengo Aug 08 '22 at 14:10
  • @CrisLuengo thank you! `what()` gives me a `Library not found` for the second part of my post. For the first one it'll still go in the exception state. This could be handled with [Gracefully handling corrupted state exceptions](https://stackoverflow.com/a/39956929/7862279) so I can find out more about the exception I guess but still without solving the issue... So I'll keep searching around. – Jack Aug 08 '22 at 16:32
  • "Library not found" is a configuration issue. Try [Dependency Walker](https://www.dependencywalker.com) on your program, it will show you which DLLs it depends on, and which ones it cannot find. It might help you fix your computer's configuration. – Cris Luengo Aug 08 '22 at 16:58

0 Answers0