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. How to avoid an object of a class to be destroyed when going out of scope in a function

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 2 Posters 812 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.
  • S Offline
    S Offline
    stordd
    wrote on 21 Feb 2020, 09:13 last edited by
    #1

    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 !

    J 1 Reply Last reply 21 Feb 2020, 09:20
    0
    • S stordd
      21 Feb 2020, 09:13

      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 !

      J Online
      J Online
      jsulm
      Lifetime Qt Champion
      wrote on 21 Feb 2020, 09:20 last edited by jsulm
      #2

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

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      S 1 Reply Last reply 21 Feb 2020, 09:27
      5
      • J jsulm
        21 Feb 2020, 09:20

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

        S Offline
        S Offline
        stordd
        wrote on 21 Feb 2020, 09:27 last edited by
        #3

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

        J 2 Replies Last reply 21 Feb 2020, 09:45
        0
        • S stordd
          21 Feb 2020, 09:27

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

          J Online
          J Online
          jsulm
          Lifetime Qt Champion
          wrote on 21 Feb 2020, 09:45 last edited by
          #4

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

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • S stordd
            21 Feb 2020, 09:27

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

            J Online
            J Online
            jsulm
            Lifetime Qt Champion
            wrote on 21 Feb 2020, 09:47 last edited by
            #5

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

            server = new TcpServer();
            

            This would be a local variable:

            TcpServer *server = new TcpServer();
            

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            S 1 Reply Last reply 21 Feb 2020, 13:28
            4
            • J jsulm
              21 Feb 2020, 09:47

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

              server = new TcpServer();
              

              This would be a local variable:

              TcpServer *server = new TcpServer();
              
              S Offline
              S Offline
              stordd
              wrote on 21 Feb 2020, 13:28 last edited by
              #6

              @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 ?

              J 1 Reply Last reply 21 Feb 2020, 13:33
              0
              • S stordd
                21 Feb 2020, 13:28

                @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 ?

                J Online
                J Online
                jsulm
                Lifetime Qt Champion
                wrote on 21 Feb 2020, 13:33 last edited by jsulm
                #7

                @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();
                

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                S 1 Reply Last reply 21 Feb 2020, 13:40
                2
                • J jsulm
                  21 Feb 2020, 13:33

                  @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();
                  
                  S Offline
                  S Offline
                  stordd
                  wrote on 21 Feb 2020, 13:40 last edited by
                  #8

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

                  J 1 Reply Last reply 21 Feb 2020, 13:44
                  0
                  • S stordd
                    21 Feb 2020, 13:40

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

                    J Online
                    J Online
                    jsulm
                    Lifetime Qt Champion
                    wrote on 21 Feb 2020, 13:44 last edited by
                    #9

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

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    S 1 Reply Last reply 21 Feb 2020, 13:56
                    0
                    • J jsulm
                      21 Feb 2020, 13:44

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

                      S Offline
                      S Offline
                      stordd
                      wrote on 21 Feb 2020, 13:56 last edited by
                      #10

                      @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 )

                      J 1 Reply Last reply 24 Feb 2020, 05:36
                      0
                      • S stordd
                        21 Feb 2020, 13:56

                        @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 )

                        J Online
                        J Online
                        jsulm
                        Lifetime Qt Champion
                        wrote on 24 Feb 2020, 05:36 last edited by
                        #11

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

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        0

                        7/11

                        21 Feb 2020, 13:33

                        • Login

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