[Solved] Program crashing when QTcpSocket receives data



  • 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");
    }

    @



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



  • 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.



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



  • 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!


Log in to reply
 

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