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. [Solved] Program crashing when QTcpSocket receives data
Qt 6.11 is out! See what's new in the release blog

[Solved] Program crashing when QTcpSocket receives data

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 3.4k Views 1 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.
  • F Offline
    F Offline
    Foreball
    wrote on last edited by
    #1

    Hello guys, I'm having an issue with QTcpSocket. When I ask to my clisock object to connect to a server and wait for incomming data, if I switch to another window in this middle time, my program will crash. I'm not using thread, just signals in the main thread.
    Below is the full code for the sample and a video which shows how to simulate this issue.

    I hope someone can help me to fix it. Thank you!

    [quote]
    Pastebin: http://pastebin.com/FYxHDZnp
    Sample project (zip): http://www.mediafire.com/?wg3g364og3hb2uh
    [/quote]

    @
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include <qtcpsocket>

    namespace Ui {
    class MainWindow;
    }

    class MySocket : public QObject
    {
    Q_OBJECT

    public:
    MySocket();
    ~MySocket();
    QTcpSocket* m_sock;
    public slots:
    void connectedx();
    void disconnectedx();
    void readyReadx();
    };

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    Ui::MainWindow *ui;
    MySocket clisock[3];
    

    private:

    public slots:
    void StartBtnClick();

    };

    #endif // MAINWINDOW_H

    #include "mainwindow.h"
    #include "ui_mainwindow.h"

    #include<qtime>

    MainWindow* g_pMainWindow = 0;

    void addlog(QString s)
    {
    g_pMainWindow->ui->LogEdit->appendPlainText(QTime::currentTime().toString("hh:mm:ss")+": " + s);
    }

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    g_pMainWindow = this;
    connect(ui->StartBtn, SIGNAL(clicked()), this, SLOT(StartBtnClick()));
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::StartBtnClick()
    {
    clisock[0].m_sock->connectToHost("208.80.152.2", 80);
    }

    // Socket events
    MySocket::MySocket()
    {
    m_sock = new QTcpSocket(this);
    connect(m_sock,SIGNAL(connected()),this,SLOT(connectedx()));
    connect(m_sock,SIGNAL(disconnected()),this,SLOT(disconnectedx()));
    connect(m_sock,SIGNAL(readyRead()),this,SLOT(readyReadx()));
    }

    MySocket::~MySocket()
    {
    delete m_sock;
    }

    void MySocket::connectedx()
    {
    addlog("connected");
    m_sock->write("GET /index.html\r\n");
    }

    void MySocket::disconnectedx()
    {
    addlog("disconnected");
    }

    void MySocket::readyReadx()
    {
    addlog("readyRead");
    }

    @

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dangelog
      wrote on last edited by
      #2

      Don't subclass QTcpSocket, and most important of all, don't OVERRIDE already existing signals with slots with the same signature!

      Software Engineer
      KDAB (UK) Ltd., a KDAB Group company

      1 Reply Last reply
      0
      • F Offline
        F Offline
        Foreball
        wrote on last edited by
        #3

        peppe, when you say to "not subclass QTcpSocket" you wanna mean I can't make a derived class of QTcpSocket and use it?

        I changed the code, now it uses different slots names, but still the same error.

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dangelog
          wrote on last edited by
          #4

          Line 50 is definitely dangerous. But please make a testcase without subclassing QTcpSocket.

          Software Engineer
          KDAB (UK) Ltd., a KDAB Group company

          1 Reply Last reply
          0
          • F Offline
            F Offline
            Foreball
            wrote on last edited by
            #5

            Thanks peppe, that line 50 was the cause of my problem. pMainWIn->activeWindow() is not really what I wanted to.
            Code is now fixed with QTcpSocket not subclassed and working fine :D
            I appreciate your help!

            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