⚠️ Forum Maintenance: Feb 6th, 8am - 14pm (UTC+2)

QMap iteration crash

  • I am using Qt 5.5 on Windows 8.1. When I run the code below, the application is able to get through one iteration, but crashes on the second one. 100% reproducible. (Copy/paste it into a Qt Creator instance and test; it might work for you).

    #include <QCoreApplication>
    #include <QDebug>
    #include <utility>
    using std::pair;
    int main(int argc, char *argv[]) {
        QCoreApplication a(argc, argv);
        QMap<QString, pair<QString, QString> > table_def = {
            {"alpha", {"INTEGER", "PRIMARY KEY"}},
            {"beta", {"VARCHAR", ""}},
            {"gamma", {"VARCHAR", ""}},
            {"delta", {"REAL", "DEFAULT 0"}},
            {"epsilon", {"INTEGER", ""}},
            {"zeta", {"INTEGER", ""}},
            {"eta", {"INTEGER", ""}},
            {"theta", {"INTEGER", ""}},
            {"iota", {"VARCHAR", ""}},
            {"kappa", {"INTEGER", "DEFAULT 0"}},
            {"lambda", {"INTEGER", "DEFAULT 0"}}
        QMapIterator<QString, pair<QString, QString> > it(table_def);
        while (it.hasNext()) {
          const QString& col_name = it.key();
          qDebug() << col_name;
          const QString& col_type = it.value().first;
          qDebug() << col_type;
          const QString& extra_def = it.value().second;
          qDebug() << extra_def;
        return a.exec();

    My Visual Studio debugger says: Unhandled exception at 0x000000006904E394 (Qt5Cored.dll) in helloqt.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.

    If it is of any relevance, I added DEFINES += Q_COMPILER_INITIALIZER_LISTS in my project file to enable initialization of QContainers with initializer lists. Also, if I replace the STL pair with QPair, the application crashes at the first iteration.

    I don't see anything wrong with this code. What could be happening?

  • Lifetime Qt Champion

    @nullstellensatz said:

    qt 5.4.1 on win 7.
    No crash with your code.

    for test, if you change all "" (empty) to something , does it still crash ?
    Just a thought since you say it crash on second. (first "")

    Code looks perfectly valid to me.

  • The first entry to be printed is actually "theta" (since the entries are not ordered by key), which has an empty string as the second value.

  • Moderators

    Which version and update of VS are you using?
    Initializer lists were infested with subtle but horrid bugs prior to VS2013 Update 3 and are not entirely standards conforming even in Update 4, although the above seems to compile and run fine with Qt5.5 in VS2013 Update 4 (the extra define seems not needed anymore?).

  • @Chris-Kawa The last time I updated Visual Studio was sometime in 2014. After getting the most recent version of Visual Studio this probelm went away.

  • Moderators


    Btw. you can check which update you're running by going to Help->About Microsoft Visual Studio.
    For example the latest update reads
    Microsoft Visual Studio 2013
    Version 12.0.31101.00 Update 4

    Btw 2. On monday (20.07.2015) VS 2015 comes out, which has greatly improved c++11 standard support.

Log in to reply