Results 1 to 3 of 3

Thread: QTcpSocket error signal emitted twice

  1. #1
    Join Date
    Nov 2012
    Posts
    4
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default QTcpSocket error signal emitted twice

    I am having an odd problem with QTcpSocket error signal. I define new socket like this:

    Qt Code:
    1. socket = new QTcpSocket(this);
    2. connect(socket, SIGNAL(readyRead()), this, SLOT(ReceivedPacket()));
    3. connect(socket, SIGNAL(error(QAbstractSocket::SocketError)),
    4. this, SLOT(connectionError(QAbstractSocket::SocketError)));
    To copy to clipboard, switch view to plain text mode 

    And everything works fine, until an error appears. Let's say I'm gonna try to connect to a host that doesn't accept connections on the port I'm trying to connect to. Error singal will be emitted, and after about 1 second there will be another one. Inside the error slot I have the following piece of code:

    Qt Code:
    1. if(socket)
    2. {
    3. delete socket;
    4. socket = NULL;
    5. }
    To copy to clipboard, switch view to plain text mode 

    But it still causes server crashes (due to a double free) and the error message is displayed twice.
    The entire code is a little bit longer, but it is available here: http://pastebin.com/9CrdK95i

    How can I prevent the error signal from being emitted/captured twice?

    Could someone please help me?
    Thank you in advance!

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,593
    Thanks
    13
    Thanked 1,587 Times in 1,516 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QTcpSocket error signal emitted twice

    You are assuming the call to connectToHost() is blocking. This is not the case. Qt networking calls are asynchronous and event driven, giving you signals when states change. The call schedules a series of events (look up host, attempt connect, connected) that will occur when the Qt event loop next processes, which does not happen until after the BCreateAccount_Clicked() routine completes. The write() call you make on the socket may queue data to be sent, but if the connect fails (#1) it may then generate a separate error (#2) because the data cannot be written.

    You are also assuming that any response will be received in one packet. This is very unlikely to be the case except if the response is very small. Your code expects only 4 bytes, but think what would happen if only 3 bytes were delivered.

    Do not delete the socket inside a slot handling signals emitted by the socket: that is a good way to flirt with a crash. Use socket->deleteLater().

  3. The following user says thank you to ChrisW67 for this useful post:

    CactusPie (21st February 2013)

  4. #3
    Join Date
    Nov 2012
    Posts
    4
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTcpSocket error signal emitted twice

    Thanks a lot! I fixed the problems you mentioned and now everything works fine.

    As for the possiblity of response not being received in one packet, I know that. The packet itself is 8 bytes long, so during testing it shouldn't really casue any problems.

    Once again, thank you.

Similar Threads

  1. Replies: 0
    Last Post: 1st July 2010, 14:11
  2. signal emitted when I zoom
    By mastupristi in forum Qwt
    Replies: 1
    Last Post: 8th July 2009, 17:02
  3. Signal emitted more than once?
    By dbrmik in forum Qt Programming
    Replies: 3
    Last Post: 13th March 2009, 12:44
  4. Program crash when a signal is emitted
    By croscato in forum Qt Programming
    Replies: 7
    Last Post: 22nd November 2008, 22:24
  5. Replies: 5
    Last Post: 9th March 2007, 12:51

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.