Slot not called on specific location of connect() call



  • Hi,

    Does anyone know why the second slot is never called? When I connect to the server, I only get the SIG 1 but not SIG 2.
    But where's the difference? Right after the first connect, the constructor of Connection leaves and execution continues right after the new, calling the second connect...

    Same with stateChanged signal. Connected in the constructor I get 1-2-3-0. Connected after new I get only 0.

    It's strange anyway, why does the connect signal even fire, especially after setSocketDescriptor? What triggers it?

    class Connection
    {
    	QSslSocket socket;
    public:
    	Connection(qintptr socketDescriptor);
    	virtual ~Connection() {};
    };
    
    Connection::Connection(qintptr socketDescriptor)
    {
    	socket.setSocketDescriptor(socketDescriptor);
    
    	QObject::connect(&socket, &QSslSocket::connected, []{ LOG("SRV SOCK CONNECTED SIG 1"); }); // slot called
    	QObject::connect(&socket, &QSslSocket::stateChanged, [](QAbstractSocket::SocketState socketState)
    	{ LOG("SRV SOCK STATECHANGED SIG 1: " << socketState); }); // slot called with socketState 1-2-3-0
    }
    
    void Server::incomingConnection(qintptr socketDescriptor)
    {
    	Connection& connection = *new Connection(socketDescriptor);
    	
    	connect(&connection.socket, &QSslSocket::connected, []{ LOG("SRV SOCK CONNECTED SIG 2"); }); // slot not called
    	connect(&connection.socket, &QSslSocket::stateChanged, [](QAbstractSocket::SocketState socketState)
    	{ LOG("SRV SOCK STATECHANGED SIG 2: " << socketState); }); // slot called, but only with socketState 0
    }
    

    Thank you and have some nice holidays!


  • Qt Champions 2016

    @qtacc32
    Hello,

        connect(&connection.socket, &QSslSocket::connected, []{ LOG("SRV SOCK CONNECTED SIG 2"); });
    

    Your socket is private, I suspect that might be a problem. One other thing that caught my eye, I hope you realize that this:

        Connection& connection = *new Connection(socketDescriptor);
    

    is a memory leak.

    Kind regards.



  • Hi,

    It's just a simple example, actually Server is a friend of Connection and there is a std::map with a std::shared_ptr containing the Connection ;-)

    Also while I don't get a connected signal at all by the incomingConnection::connect call, I do get a stateChanged signal, but only with state 0 on disconnect..
    (I'll update the code on this)

    Thank you!



  • I'm pushing this.

    It's strange, isn't it?


  • Qt Champions 2016

    @qtacc32
    If you're still struggling with this if possible try sharing a real piece of the code. It might be something trivial that doesn't show up in the example (which looks fine). Or at least a MWE that reproduces the problem, since I could not run your example, since it's not complete.

    Kind regards.



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