Important: Please read the Qt Code of Conduct -

QCoreApplication conflicts with Matlab when used in Release but not Debug mode

  • I'm developing some software under 64 bit Windows 7 using Visual Studio C++ 2010. I downloaded and built a 64 bit version of Qt 4.8.4 using C++. All has been working well. As part of the development I need to access a Matlab module from a C++-based Qt project. To establish that things would work I built a test program which accessed the Matlab DLL from C++ code.

    The program fits the function: z=a+bexp(((c-x)/d)**2)((e-y)/f)**2)) and works perfectly in MATLAB.

    The program invoking the MATLAB DLL is as follows:
    #include <QtCore/QCoreApplication>
    #include "libgaussfit.h"
    #include <string>
    #include <iostream>
    #include "mclcppclass.h"
    #include <QTime>

    using namespace std;

    int main(int argc, char *argv[])
    QCoreApplication a(argc, argv);

    // omitted - code that generates data to be fitted
    double** surf = new double*[3];

    for(int k = 0; k < 3; k++)
    surf[k] = new double[nNoPixels];
    // omitted code that generates surf for each of three cases

    // I wanted to time events
    QTime t;

    cerr << "Initalizing MATLAB\n";
    if (!mclInitializeApplication(NULL,0))
        cerr << "Could not initialize the application.\n";
        return -1;
    // Initialize the library
    if( !libgaussfitInitialize())
        cerr << "Could not initialize the library\n";
        return -1;
    cerr << "Initialization took " << t.elapsed() << "msec\n";
    mwArray indata(nNoPixels, 1, mxDOUBLE_CLASS);
    mwArray bsize(1, 1, mxDOUBLE_CLASS);
    mwArray fcoeff(1, 6, mxDOUBLE_CLASS);
    mwArray frsq(1, 1, mxDOUBLE_CLASS);
    mwArray fsse(1, 1, mxDOUBLE_CLASS);
    double sse;
    double rsquare;
    double coeff[6];
    for(int m = 0; m < 3; m++)
       cerr << "Fiting curve #" << (m+1) << "\n";
       indata.SetData(surf[m], nNoPixels);
       gaussfit(3, fcoeff, frsq, fsse, bsize, indata);
       fcoeff.GetData(coeff, 6);
       fsse.GetData(&sse, 1);
       frsq.GetData(&rsquare, 1);
       cerr << "a = " << coeff[0] << "\nb = " << coeff[1] << "\nc = " << coeff[2] << "\nd = " << coeff[3] << "\ne = " << coeff[4] \
             << "\nf = " << coeff[5] << "\nr sq = " << rsquare << "\nsse = " << sse << "\n";
       cerr << "Fit took " << t.elapsed() << "msec\n";

    This is a console application on the x_64 platform. It works perfectly when run under the Visual C++ 'Debug' mode and produces the following output:
    Initializing MATLAB
    Initialization took 4451 msecs
    Fitting curve #1
    a = 215.5
    b = 3150
    c = 6.76
    d = 1.742
    e = 9.21
    f = 0.934
    r sq = 1
    sse = 4.72559e-025
    Fit took 105 msec
    ...etc.which is the right result for my test data.

    When I try to run it in 'Release' mode I get the following crash:

    Initializing MATLAB
    Initialization took 2122 msecs
    Fitting curve #1
    Caught "std::exception" Exception message is :
    libmwblas: load error: mkl.dll

    Clearly this program can run as a non-Qt program and when I did it worked perfectly in both Debug and Release mode. A little more experimentation showed that it was the presence of the QCoreApplication alone that was generating the error in Release mode. I'm aware that the Debug and Release versions of Qt are compiled differently (although the code is the same?) and that this might be the source of the difference, though how it may be causing this problem is a puzzle to me.

    I'd be grateful for any ideas, and my apologies for the length of this post.

  • Do you really have a mkl.dll and not something like mkl_d.dll?

  • AFAIK I have neither - at least I haven't found either one doing a search. In any case it is not the absence of a library that is causing the problem - the program works perfectly (in both Debug and Release modes) when I remove the QCoreApplication statement.

  • Did you find a solution for this. I get the exact same error when I call my Matlab compiled c++ library from Visual Studio.
    It initializes correctly but throws an error about missing mkl.dll during function call.
    Funny thing is I am able to call this function from a console project in visual studio without error. But I get an error when calling it from the static library.

Log in to reply