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. std::bad_alloc error while appending to QString

std::bad_alloc error while appending to QString

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 1.7k Views 2 Watching
  • 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.
  • A Offline
    A Offline
    Ali Rashidi
    wrote on last edited by
    #1

    Hi everyone and thanks for your care to this question.

    I`m writing a class, that gets a base URL, and 2 QStringLists of parametr values and names, then appends them to the base url to make a working URL, send request and get reply.

    The problem is, the program crashes whie appending to the finalURL. see this code:

    void NetworkObject::makeURL()
    {
        _finalURL.append(_baseURL);
        _finalURL.append("?");
        qDebug() << _finalURL.toLatin1();
        for (int i = 0; i < _varNameList.size(); i++)
        {
            _finalURL.append(_varNameList[i]);
            _finalURL.append("=");
            _finalURL.append(_varValueList[i]); //##
            if (i =! _varNameList.size() - 1)
            {
                _finalURL.append("&");
            }
        }
    }
    

    Using debugger, I found it occurs on the line marked with ##.

    Something makes me amazed and that is, I made a mistake before, and instead of that if statement, just wrote:

    _finalURL.append("&");
    

    and it had no error in this scope.

    M 1 Reply Last reply
    0
    • A Ali Rashidi

      Hi everyone and thanks for your care to this question.

      I`m writing a class, that gets a base URL, and 2 QStringLists of parametr values and names, then appends them to the base url to make a working URL, send request and get reply.

      The problem is, the program crashes whie appending to the finalURL. see this code:

      void NetworkObject::makeURL()
      {
          _finalURL.append(_baseURL);
          _finalURL.append("?");
          qDebug() << _finalURL.toLatin1();
          for (int i = 0; i < _varNameList.size(); i++)
          {
              _finalURL.append(_varNameList[i]);
              _finalURL.append("=");
              _finalURL.append(_varValueList[i]); //##
              if (i =! _varNameList.size() - 1)
              {
                  _finalURL.append("&");
              }
          }
      }
      

      Using debugger, I found it occurs on the line marked with ##.

      Something makes me amazed and that is, I made a mistake before, and instead of that if statement, just wrote:

      _finalURL.append("&");
      

      and it had no error in this scope.

      M Offline
      M Offline
      mjsurette
      wrote on last edited by
      #2

      @Ali-Rashidi
      I would have

      _finalURL = _baseURL;
      

      as the first line. That way you're sure that you know _finalURL is not filled with results from previous calls. std::bad_alloc would suggest that you're running out of memory. These days that's pretty hard to do.

      As an aside, did you consider using QStringList::join?

      Mike

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

        Hi

        The if looks wrong to me

        if (i =! _varNameList.size() - 1)

        I would expect

        if ( i ! = _varNameList.size() - 1)

        Also as Mike says, it sounds like out of memory but URLs are normally not that big.
        Have you tried qDebug() << varNameList[i]; to see what u are appending?
        (in the loop)

        A 1 Reply Last reply
        2
        • mrjjM mrjj

          Hi

          The if looks wrong to me

          if (i =! _varNameList.size() - 1)

          I would expect

          if ( i ! = _varNameList.size() - 1)

          Also as Mike says, it sounds like out of memory but URLs are normally not that big.
          Have you tried qDebug() << varNameList[i]; to see what u are appending?
          (in the loop)

          A Offline
          A Offline
          Ali Rashidi
          wrote on last edited by
          #4

          @mrjj Oh I forgot that, however, I still faced SEGFAULT using this approach in another part, so changed the method completely, thanks for help

          1 Reply Last reply
          1

          • Login

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