MATLAB: Matlab::da​ta::ArrayF​actory exception: “attempted to assign clientFactoryFuncPtr twice to different values”

cpplib arrayfactoryMATLAB Compiler SDK

I used the Matlab Library Compiler to access Matlab functions from a C++ shared library.
When instantiating ArrayFactory, the execution either hangs or throws the exception "attempted to assign clientFactoryFuncPtr twice to different values"; the behavior depends on when ArrayFactory is instantiated.
Calling initMATLABLibrary() doesn't seem to create any issues. Also compiling and executing a sample cpp program created with the Library Compiler works, hence I don't think there is a general problem with my build environment.
The following function is called by matlab::cpplib::runMain():
int f(std::shared_ptr<matlab::cpplib::MATLABApplication> app, const int argc, const char * argv[])
{
try
{
// Throws exception
matlab::data::ArrayFactory factory;
auto ml = matlab::cpplib::initMATLABLibrary(app, u"libmatlab_procesing.ctf");
// Instantiation after initMATLABLibrary() hangs, but no exception is thrown
//matlab::data::ArrayFactory factory;
// ...
}
catch(const std::exception & exc)
{
std::cerr << exc.what() << std::endl;
return -1;
}
return 0;
}
Is there any explanation for the behavior of ArrayFactory?

Best Answer

Contrary to our first assesment, also the generated sample code does not work flawlessly: It just fails silently, because of the structure of the sample script, and because exceptions are not caught in the function called by runMain().
After further investigation we now see that the behavior described above is introduced by linking to "libMatlabDataArray.so" of the installed Matlab Runtime. Linking against the library provided by the Matlab installation seems to solve the issue.