Important: Please read the Qt Code of Conduct -

QtConCurrent::map always crash for simple multi-thread program

  • os : mac osx 10.8.3
    compiler: clang3.2
    Qt version: 5.1

    #ifndef LOADIMAGES_HPP
    #define LOADIMAGES_HPP

    #include <QMutex>
    #include <QObject>

    class QString;
    class QStringList;

    class loadImages : public QObject
    explicit loadImages(QObject *parent = nullptr);
    loadImages(loadImages const&) = delete;
    loadImages& operator=(loadImages const&) = delete;

    void loadImagesImpl_2(QStringList const &names);
    void loadImagesImpl(QString const &name);

    QMutex mutex_;

    #endif // LOADIMAGES_HPP

    #include <functional>

    #include <QDebug>

    #include <QtConcurrent/QtConcurrentMap>
    #include <QtConcurrent/QtConcurrentRun>
    #include <QDir>
    #include <QImage>
    #include <QMutexLocker>
    #include <QStringList>

    #include "loadImages.hpp"

    loadImages::loadImages(QObject *parent) : QObject(parent)
    namespace ph = std::placeholders;
    QStringList names;

    for(auto const &data: names){
        QImage img(data);
        qDebug()<<names<<" is null : "<<img.isNull();
    QtConcurrent::map(names, std::bind(&loadImages::loadImagesImpl, this, ph::_1)); //always crash
    //QtConcurrent::run(this, &loadImages::loadImagesImpl_2, names); //work fine


    void loadImages::loadImagesImpl_2(QStringList const &names)
    for(auto const &data: names){
    QImage img(data);

    void loadImages::loadImagesImpl(QString const &name)
    //I don't think we need a mutex at here, but I want to give this "workaround" a try
    //no matter I add this mutex or not, the program always crash
    QMutexLocker lock(&mutex_);

    QImage img(name);


    #include <QApplication>
    #include <QLabel>

    #include "loadImages.hpp"

    int main(int argc, char *argv[])
    QApplication app(argc, argv);

    loadImages load;
    QLabel label;
    label.setMinimumSize(400, 400);;
    return app.exec(&#41;;



    The program always crash if I call QtConcurrent::Map, but QtConcurrent::run work fine
    What kind of faults do I make?Can't find any error

Log in to reply