Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. read access violation when splitting QString in QVariantMap initialization
QtWS25 Last Chance

read access violation when splitting QString in QVariantMap initialization

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 4 Posters 2.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • zekroZ Offline
    zekroZ Offline
    zekro
    wrote on last edited by
    #1

    I'm working on a project where I need to parse a status log list. So I take the lines and split them to get the information which I put into a QVariantMap.

    The code for this looks like following:

    ifstream fs(DIR_STATUSFILE);
    string line;
    if (fs.is_open()) {
        while (getline(fs, line)) {
            QStringList split = QString::fromStdString(line).split(",");
            int mark_ind = split.indexOf("STATUS");
            qDebug() << split[mark_ind + 3].split("|")[0].toInt();
            lines.insert(split[0], QVariantMap({
                {"dir", split[0]},
                {"status", split[mark_ind + 2]},
                {"port", split[mark_ind + 3].split("|")[0].toInt()}
            }));
        }
        fs.close();
    }
    

    Now, I'm getting the following exception:

    The inferior stopped because it triggered an exception.
    Stopped in thread 0 by: Exception at 0x6b49d69a, code: 0xc0000005: read access violation at: 0x0, flags=0x0 (first chance).
    

    But the debug output gives the port number, so the index and the value exists and is not a null pointer.

    But when I execute the split (split[mark_ind + 3].split("|")[0].toInt()) above the initialization in an extra variable and then put it into the QVariantMap, it works.

    ifstream fs(DIR_STATUSFILE);
    string line;
    if (fs.is_open()) {
        while (getline(fs, line)) {
            QStringList split = QString::fromStdString(line).split(",");
            int mark_ind = split.indexOf("STATUS");
            int port = split[mark_ind + 3].split("|")[0].toInt();
            lines.insert(split[0], QVariantMap({
                {"dir", split[0]},
                {"status", split[mark_ind + 2]},
                {"port", port}
            }));
        }
        fs.close();
    }
    

    Now, I just want to know what's wrong there and what exactly causes this error. Maybe, this is a bug?

    JonBJ J.HilkJ 2 Replies Last reply
    0
    • zekroZ zekro

      I'm working on a project where I need to parse a status log list. So I take the lines and split them to get the information which I put into a QVariantMap.

      The code for this looks like following:

      ifstream fs(DIR_STATUSFILE);
      string line;
      if (fs.is_open()) {
          while (getline(fs, line)) {
              QStringList split = QString::fromStdString(line).split(",");
              int mark_ind = split.indexOf("STATUS");
              qDebug() << split[mark_ind + 3].split("|")[0].toInt();
              lines.insert(split[0], QVariantMap({
                  {"dir", split[0]},
                  {"status", split[mark_ind + 2]},
                  {"port", split[mark_ind + 3].split("|")[0].toInt()}
              }));
          }
          fs.close();
      }
      

      Now, I'm getting the following exception:

      The inferior stopped because it triggered an exception.
      Stopped in thread 0 by: Exception at 0x6b49d69a, code: 0xc0000005: read access violation at: 0x0, flags=0x0 (first chance).
      

      But the debug output gives the port number, so the index and the value exists and is not a null pointer.

      But when I execute the split (split[mark_ind + 3].split("|")[0].toInt()) above the initialization in an extra variable and then put it into the QVariantMap, it works.

      ifstream fs(DIR_STATUSFILE);
      string line;
      if (fs.is_open()) {
          while (getline(fs, line)) {
              QStringList split = QString::fromStdString(line).split(",");
              int mark_ind = split.indexOf("STATUS");
              int port = split[mark_ind + 3].split("|")[0].toInt();
              lines.insert(split[0], QVariantMap({
                  {"dir", split[0]},
                  {"status", split[mark_ind + 2]},
                  {"port", port}
              }));
          }
          fs.close();
      }
      

      Now, I just want to know what's wrong there and what exactly causes this error. Maybe, this is a bug?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @zekro
      I have no idea, but maybe the compiler needs a little help with your types?

      http://doc.qt.io/qt-5/qvariant.html#QVariantMap-typedef:

      typedef QVariantMap
      Synonym for QMap<QString, QVariant>.

      So the 2nd argument in your list items is supposed to be QVariant. You are passing the first two as QString but port as int. I'm not sure whether you're supposed to be allowed to mix types like that in a C++ {} initialization list, or the compiler/code generator gets confused. Maybe you need some explicit QVariant casting?

      1 Reply Last reply
      0
      • zekroZ Offline
        zekroZ Offline
        zekro
        wrote on last edited by zekro
        #3

        @JonB
        Thaks for the answer!

        Yes, I've just tried to just enter the value as QString or QStringList, but it fails at both.

        lines.insert(split[0], QVariantMap({
        	{"dir", split[0]},
        	{"status", split[mark_ind + 2]},
        	{"port", split[mark_ind + 3].split("|")}
        }));
        

        I'll create a testing branch for this and will try the type solution later. Results in next answer ;)

        JonBJ 1 Reply Last reply
        0
        • zekroZ zekro

          @JonB
          Thaks for the answer!

          Yes, I've just tried to just enter the value as QString or QStringList, but it fails at both.

          lines.insert(split[0], QVariantMap({
          	{"dir", split[0]},
          	{"status", split[mark_ind + 2]},
          	{"port", split[mark_ind + 3].split("|")}
          }));
          

          I'll create a testing branch for this and will try the type solution later. Results in next answer ;)

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @zekro
          Correct me if I'm wrong, but the first 2 return a QString while the third returns a QStringList. I think they may all need to be the same type when in in an initialization list...? (Name your variable something other than split if you're going to be using the QString::split() function on it, for clarity!)

          BTW, if you want to see if it's an issue with initializer list, get rid of {} initializer and assign each of the 3 elements explicitly....

          1 Reply Last reply
          1
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi
            You also need to check your index.
            int port = split[mark_ind + 3].split("|")[0].toInt(); // Might crash if "|" not there
            and so on.
            SO make code better by checking index BEFORE access.

            JonBJ 1 Reply Last reply
            2
            • mrjjM mrjj

              Hi
              You also need to check your index.
              int port = split[mark_ind + 3].split("|")[0].toInt(); // Might crash if "|" not there
              and so on.
              SO make code better by checking index BEFORE access.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @mrjj
              I think OP's point is that he's claiming he has tried the code explicitly (in the debugger) and the indexes are valid, but behaviour depends on how he writes the code. And that's his question. (Of course he should be checking indexes, but he's saying they are not at fault here. And I can only take his word for it :) )

              mrjjM 1 Reply Last reply
              1
              • JonBJ JonB

                @mrjj
                I think OP's point is that he's claiming he has tried the code explicitly (in the debugger) and the indexes are valid, but behaviour depends on how he writes the code. And that's his question. (Of course he should be checking indexes, but he's saying they are not at fault here. And I can only take his word for it :) )

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by mrjj
                #7

                @JonB
                Yes, i think u are right after re-reading first post :)
                Its more about initialization lists.

                1 Reply Last reply
                0
                • zekroZ zekro

                  I'm working on a project where I need to parse a status log list. So I take the lines and split them to get the information which I put into a QVariantMap.

                  The code for this looks like following:

                  ifstream fs(DIR_STATUSFILE);
                  string line;
                  if (fs.is_open()) {
                      while (getline(fs, line)) {
                          QStringList split = QString::fromStdString(line).split(",");
                          int mark_ind = split.indexOf("STATUS");
                          qDebug() << split[mark_ind + 3].split("|")[0].toInt();
                          lines.insert(split[0], QVariantMap({
                              {"dir", split[0]},
                              {"status", split[mark_ind + 2]},
                              {"port", split[mark_ind + 3].split("|")[0].toInt()}
                          }));
                      }
                      fs.close();
                  }
                  

                  Now, I'm getting the following exception:

                  The inferior stopped because it triggered an exception.
                  Stopped in thread 0 by: Exception at 0x6b49d69a, code: 0xc0000005: read access violation at: 0x0, flags=0x0 (first chance).
                  

                  But the debug output gives the port number, so the index and the value exists and is not a null pointer.

                  But when I execute the split (split[mark_ind + 3].split("|")[0].toInt()) above the initialization in an extra variable and then put it into the QVariantMap, it works.

                  ifstream fs(DIR_STATUSFILE);
                  string line;
                  if (fs.is_open()) {
                      while (getline(fs, line)) {
                          QStringList split = QString::fromStdString(line).split(",");
                          int mark_ind = split.indexOf("STATUS");
                          int port = split[mark_ind + 3].split("|")[0].toInt();
                          lines.insert(split[0], QVariantMap({
                              {"dir", split[0]},
                              {"status", split[mark_ind + 2]},
                              {"port", port}
                          }));
                      }
                      fs.close();
                  }
                  

                  Now, I just want to know what's wrong there and what exactly causes this error. Maybe, this is a bug?

                  J.HilkJ Online
                  J.HilkJ Online
                  J.Hilk
                  Moderators
                  wrote on last edited by
                  #8

                  @zekro additionaly to what the others said, you could try to explicitly create a QVariant that is used in the QVariantMap initialization

                  ifstream fs(DIR_STATUSFILE);
                  string line;
                  if (fs.is_open()) {
                  while (getline(fs, line)) {
                  QStringList split = QString::fromStdString(line).split(",");
                  int mark_ind = split.indexOf("STATUS");
                  qDebug() << split[mark_ind + 3].split("|")[0].toInt();
                  lines.insert(split[0], QVariantMap({
                  {"dir", QVariant::fromValue(split[0])},
                  {"status", QVariant::fromValue(split[mark_ind + 2])},
                  {"port", QVariant::fromValue(split[mark_ind + 3].split("|")[0].toInt())}
                  }));
                  }
                  fs.close();
                  }


                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  JonBJ 1 Reply Last reply
                  0
                  • J.HilkJ J.Hilk

                    @zekro additionaly to what the others said, you could try to explicitly create a QVariant that is used in the QVariantMap initialization

                    ifstream fs(DIR_STATUSFILE);
                    string line;
                    if (fs.is_open()) {
                    while (getline(fs, line)) {
                    QStringList split = QString::fromStdString(line).split(",");
                    int mark_ind = split.indexOf("STATUS");
                    qDebug() << split[mark_ind + 3].split("|")[0].toInt();
                    lines.insert(split[0], QVariantMap({
                    {"dir", QVariant::fromValue(split[0])},
                    {"status", QVariant::fromValue(split[mark_ind + 2])},
                    {"port", QVariant::fromValue(split[mark_ind + 3].split("|")[0].toInt())}
                    }));
                    }
                    fs.close();
                    }

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #9

                    @J.Hilk
                    Yes indeed, though I suspect OP doesn't want to write that on each line. His question remains why one way of writing his code works and another faults.

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved