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. Quint16 zero crash

Quint16 zero crash

Scheduled Pinned Locked Moved General and Desktop
6 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.
  • A Offline
    A Offline
    Audiocrow
    wrote on last edited by
    #1

    I'm reading in a block size for network code, using a quint16 to store the block size. I start the blockSize off as 0, and it's read in when the readMessage() slot is called from the socket's readyRead() signal. The program instantly crashes as soon as readMessage() is called, before the first line even runs. However, if I initialize blockSize as 1 instead of 0, there is no crash at all. The blockSize is never read in if I do this, though, so there are problems. This is an incredibly stupid bug and I don't know what the problem is.

    @
    class cPlayer
    {

    friend class MainServer;

    private:
    int id;
    QTcpSocket* socket;
    quint16 blockSize;
    public:
    cPlayer() { blockSize = 0; }
    };
    @

    Here's the code where the connections are setup. The message that is sent goes through, the client responds, and then the server crashes. The first line of readMessage is (qDebug() << "Message Received") and it doesn't happen.
    @
    void MainServer::handleNewConnection()
    {
    while(server->hasPendingConnections()) {
    cPlayer* newPlayer = new cPlayer;
    newPlayer->init();
    newPlayer->id = numPlayers++;
    newPlayer->socket = server->nextPendingConnection();
    connect(newPlayer->socket, SIGNAL(disconnected()), this, SLOT(handleDisconnection()));
    QSignalMapper* sMap = new QSignalMapper(this);
    sMap->setMapping(newPlayer->socket, newPlayer->id);
    connect(newPlayer->socket, SIGNAL(readyRead()), sMap, SLOT(map()));
    connect(sMap, SIGNAL(mapped(int)), this, SLOT(readMessage(int)));
    playerList.append(newPlayer);
    appendPlainText(QString("[%1]%2 connected - ID %3\n").arg(QTime::currentTime().toString()).arg(newPlayer->socket->peerAddress().toString()).arg(numPlayers-1));
    sendMessage(newPlayer->id, 0);
    }
    }
    @

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Could you post a bit more code ? We don't know how sendMessage works nor readMessage and there might lie your problem.

      On an unrelated note, one thing that you could avoid is recreating sMap each time, you don't need one mapper per connection.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Serenity
        wrote on last edited by
        #3

        In line 7:
        [code]newPlayer->socket = server->nextPendingConnection();[/code]

        What happen, when nextPendingConnection()==NULL ?

        1 Reply Last reply
        0
        • T Offline
          T Offline
          tzander
          wrote on last edited by
          #4

          Interestingly, you mention the quint16, but you never use it in any code (except for setting it to zero).

          I suggest using a debugger or something like valgrind to find out where the crash comes from.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            Audiocrow
            wrote on last edited by
            #5

            Here is the sendMessage code:

            @
            void MainServer::readMessage(int id)
            {
            appendPlainText("This line is never being hit");
            cPlayer* Player = playerAt(id);
            if(!Player)
            return;
            QDataStream in(Player->socket);
            in.setVersion(QDataStream::Qt_4_8);
            if(Player->blockSize == 0) {
            if(Player->socket->bytesAvailable() < (int)sizeof(quint16))
            return;
            in >> Player->blockSize;
            appendPlainText(QString("Block Size:%1").arg(Player->blockSize));
            }
            if(Player->socket->bytesAvailable() < (quint16)Player->blockSize)
            return;
            quint16 msgid;
            in >> msgid;
            appendPlainText(QString("Received Message %1\n").arg(msgid));
            switch(msgid) {
            case 0:
            sendMessage(id, 1);
            break;
            default:
            Player->blockSize = 0;
            }
            }
            @

            1 Reply Last reply
            0
            • A Offline
              A Offline
              Audiocrow
              wrote on last edited by
              #6

              Figured it out, dumb mistake. I was sending a map that didn't exist. I assumed readMessage was failing to run because I wasn't seeing my appended text, but it was just because the crash was happening before Qt's update event.

              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