Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to avoid an object of a class to be destroyed when going out of scope in a function



  • Hello,
    I'm working with TcpServer and Client on QT4.7
    I have a class TcpServer that creates a server and a client. My server connects to a distant client , receives data from it and send it to the distant server via my client. My client connects to a distant server, receives data from it and send it to the distant client via my server (my class is acting as a spy between the distant server and the distant client). Everything works fine when i had this in my main.cpp :

    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        TcpServer server;
        server.connectToNrs();
        server.connectToSncs();
        return a.exec();
    }
    

    and in my TcpServer.cpp :

    void TcpServer::connectToNrs()
    {
        _serverNrs = new QTcpServer(this);
        connect(_serverNrs, SIGNAL(newConnection()), this, SLOT(newConnection()));
        _serverNrs->listen(QHostAddress("192.168.x.x"), port1);
        qDebug() << "Listening to connection from nrs";
    }
    
    void TcpServer::connectToSncs()
    {
        _socketTechnique = new QTcpSocket(this);
        connect(_socketTechnique, SIGNAL(readyRead()), this, SLOT(getSNCS()));
        _socketTechnique->connectToHost(QHostAddress("192.168.x.x"), port2);
        if(_socketTechnique->waitForConnected(3000) == true)
        {
            qDebug() << "Client connected to technical sncs";
        }
        else {
            qDebug() << "Client not connected to technical sncs";
        }
    }
    void TcpServer::newConnection()
    {
        _socketServer = new QTcpSocket(this);
        connect(_socketServer, SIGNAL(readyRead()), this, SLOT(getNRS()));
        _socketServer = _serverNrs->nextPendingConnection();
        qDebug() << QString( "Incoming connection from %1" ).arg(_socketServer->peerAddress().toString());
        qDebug() << "Server connected to NRS";
    }
    

    But now I added an IHM that, when i click on the button launch, would execute the same actions than in my main.cpp if a boolean is true and creates an other client that would connect to and other server if the boolean is false.

    my IHM.cpp :

    void IHM::on_buttonLaunch_clicked()
    {
        ui->buttonLaunch->setEnabled(false);
        launchApp();
        ui->buttonLaunch->setEnabled(true);
    }
    void IHM::launchApp()
    {
        server = new TcpServer();
        if (button ==true)
        {
            server->connectToNrs();
            server->connectToSncs();
        }
        else {
    
            server->connectToNrs();
            server->connectToSncs();
            server->connectToSNCS2();
        }
    }
    

    now my main.cpp looks like that :

    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        IHM ihm;
        ihm.show();
        return a.exec();
    }
    

    And it's not working anymore. I understand that when my server goes out of scope in my function launchApp() it is destroyed and so the execution fails but I can't seem to find a way around this so if someone could help me it's would be very nice. Thank you !


  • Lifetime Qt Champion

    @stordd Either make that local variable a class member or allocate it on the heap (but then you have to make sure it is deleted when not needed anymore)...
    Update: actually your server is not going out of scope as it is allocated on the heap. There must be something else wrong in your code.



  • @jsulm Ok thank you I'm going to try that !


  • Lifetime Qt Champion

    @stordd Local variable going out of scope is not the issue in your code. You're allocating on the heap, so it is NOT destroyed when the method finishes. It must be something else in your code.


  • Lifetime Qt Champion

    @stordd Also, your server is NOT a local variable apparently:

    server = new TcpServer();
    

    This would be a local variable:

    TcpServer *server = new TcpServer();
    


  • @jsulm when i add

    TcpServer *server = new TcpServer();
    

    in my launchApp() function sometimes my program exit in my slot getSNCS() after doing half of the work and sometimes it exits right away. Any ideas ?


  • Lifetime Qt Champion

    @stordd said in How to avoid an object of a class to be destroyed when going out of scope in a function:

    when i add
    TcpServer *server = new TcpServer();

    in my launchApp() function sometimes my program exi

    I didn't say you should do that! This would make server a local variable (would shadow the class member called server) and thus would be wrong!
    Please read more carefully.
    What I said is: you do not have a local variable in your code, so this is not the source of your problem. It must be something else.

    " sometimes my program exit in my slot getSNCS()" - of cource is does as now you're accessing a not initialised member variable server. So, do not do

    TcpServer *server = new TcpServer();
    

    this is correct

    server = new TcpServer();
    


  • @jsulm Yes I know but I'm new to programming so I'm trying everything, I know that's not the good way but I have no idea how to solve that.


  • Lifetime Qt Champion

    @stordd You need to debug your app. For example: what happens in server->connectToNrs() and server->connectToSncs()?



  • @jsulm It's weird I'm debugging step by step and when it'sdoing the server.connectToSncs() and server.connectToNrs() it goes to the moc_IHM.cpp at int IHM::qt_metacall() and then go back to the main.cpp and get stuck on the line a.exec()
    And my functions connectTosncs() and connectToNrs() are executing (I have some qDebug inside )


  • Lifetime Qt Champion

    @stordd Do you see any of the qDebug outputs you have in your code? Is the connection established?


Log in to reply