Program crash when using MuparserX with QThreadPool

172 views Asked by At

I want to run multiple MuparserX parsers concurrently using QThreadPool. Here's the code:

#include <iostream>
#include <QRunnable>
#include <QThreadPool>
#include "mpParser.h"

struct Task: public QRunnable {
    void run() override {
        //Create a new parser
        mup::ParserX p;
    }
};

int main(int argc, char *argv[])
{
    for (int i = 0; i != 10; ++i)
        QThreadPool::globalInstance()->start(new Task);

    while (QThreadPool::globalInstance()->activeThreadCount() > 0) {}

    return 0;
}

However, my program crashes with either "list iterators incompatible" or "list iterator not dereferencable" errors in the destructor mup::ParserX::~ParserX(). This happens only with MSVC13 and 15, and only in debug builds; release builds run without error and produce the expected output. GCC debug and release builds both work fine. Is there some nuance of Microsoft's compiler that's causing this, or is my program incorrect?

1

There are 1 answers

0
Carlton On

I have found a work-around for this problem, though not really a clean solution. The error occurs in mup::IToken::~IToken(), when the macro MUP_LEAKAGE_REPORT is defined. The macro (automatically defined in debug builds) invokes a static member in a non-thread-safe way, giving rise to the iterator error. The simple solution is to just comment out the line where the macro is defined. In my version of MuparserX, it is in mpDefines.h, line 100:

  #define MUP_LEAKAGE_REPORT

As far as I can tell, the macro is only used for debugging MuparserX and doesn't have any effect on the behavior of the parser.