Problem with QStringList



  • Consider the following:

      // create the message log header
      std::ostringstream out;
      out << "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>"
          << std::endl
          << "<TapMessagingLog start_time=\"" << ACE_OS::gettimeofday().sec()
          << "\">" << std::endl;
    
      if (remote_logging)
      {
        // we can't log messages until we have a connection to the service, so
        // pend the header message for now
        pending_msglog_messages.push_back(QString::fromStdString(out.str()));
      }
    

    Then later...

    void TapDisplaySingleton::unwindMsgLogMessages()
    {
      //DAR_DEBUG
      QString test = "abcdefg";
      pending_msglog_messages.push_back(test);
      QString test2 = pending_msglog_messages.at(1);
      //DAR_DEBUG
    
      while(!pending_msglog_messages.isEmpty())
      {
        QString out_str = pending_event_log_messages.takeFirst();
    

    When I try to pull item 0 from the QStringList, the code throws an assertion, but in my debug code, I create a simple string, push it onto the QStringList and pull it off again, and it works fine. It clearly does not play nicely with the first string, but I've never had any problems working with it before trying to put it into a QStringList

    Any ideas?


  • Moderators

    @DRoscoe

    It is not really clear where the assertion is thrown and you didn't say which assertion.
    Is it here:

    pending_msglog_messages.push_back(test);
    QString test2 = pending_msglog_messages.at(1);
    

    ?
    If so, then it could be that pending_msglog_messages contains only one element and you try to access the second calling at(1).
    Shouldn't it be

    QString test2 = pending_msglog_messages.at(0);
    

    You can print the number of elements in the list to see how many elements it contains.
    ?


  • Qt Champions 2016

    @DRoscoe said in Problem with QStringList:

    while(!pending_msglog_messages.isEmpty())
    {
       QString out_str = pending_event_log_messages.takeFirst();
    

    A typo? I'd expect the condition to be !pending_event_log_messages.isEmpty(). It appears you're judging for the availability of items in one list/vector by the elements in another, and if for some reason there's a mismatch ...


  • Moderators

    @kshegunov That's what code reviews are good for: more eyes see more :-)



  • @kshegunov Oh dear God.. I got code blind! Copy and paste error.

    Thanks!


  • Qt Champions 2016

    @DRoscoe
    No hassle, it happens. :)


  • Qt Champions 2016

    @DRoscoe
    For any productive programmer it will happen sometimes :)
    My all time favorite is

    class Main
    SomeClass* mX;

    then somewhere else
    SomeClass* mX = new SomeClass()

    And go doh a little later. :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.