Results 1 to 4 of 4

Thread: [QTcpSocket] disconnectFromHost in a class Destructor

  1. #1
    Join Date
    May 2010
    Location
    Rousse, Bulgaria
    Posts
    25
    Thanks
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default [QTcpSocket] disconnectFromHost in a class Destructor

    Hello! I'm using QTcpSocket to send messages to a remote server.
    Here's a quick example to give you a heads-up:

    Qt Code:
    1. Myclass::Myclass(QWidget *parent) : QWidget(parent)
    2. {
    3. QTcpSocket *socket = new QTcpSocket; // To know what socket is
    4. // Connect to server and launch session:
    5. socket->connectToHost("server.example.com",12345);
    6. socket->write("Session: self;Start");
    7. // OK, Session is connected.
    8. // If I call this:
    9. socket->write("Session: self; Disconnect");
    10. // The session closes successfully.
    11. }
    To copy to clipboard, switch view to plain text mode 
    However...
    Qt Code:
    1. Myclass::~Myclass() {
    2. socket->write("Session: self; Disconnect");
    3. qDebug() << "Program closing...";
    4. }
    To copy to clipboard, switch view to plain text mode 

    On closing my application (Alt+F4, closing the window), the debug console says:
    Program closing...
    but the socket is not sending the message to the server and my session remains opened.

    If I do this:

    Qt Code:
    1. Myclass::~Myclass()
    2. {
    3. socket->write("Session: this; Disconnect");
    4. QMessageBox::warning(this,"Closing", "Program is closing...");
    5. qDebug() << "Program closed.";
    6. }
    To copy to clipboard, switch view to plain text mode 

    And voila, it works. it sends the message, pops up the warning box, and prints the message. Is there a way to make it work without the Messagebox ?

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: [QTcpSocket] disconnectFromHost in a class Destructor

    Your QTcpSocket object does not have a parent, and hence will cause a memory leak in you case. You should be doing like this

    Qt Code:
    1. QTcpSocket *socket = new QTcpSocket(this)
    To copy to clipboard, switch view to plain text mode 

    This will ensure that when MyClass is destroyed, QTcpSocket is also properly destroyed.

    This should also solve your problem, as when destroying the QTcpSocket, all the pending data is flushed you of the socket and then it is deleted

  3. The following 2 users say thank you to Santosh Reddy for this useful post:

    Axtroz (6th June 2011), Marijn.88 (8th June 2011)

  4. #3
    Join Date
    May 2010
    Location
    Rousse, Bulgaria
    Posts
    25
    Thanks
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [QTcpSocket] disconnectFromHost in a class Destructor

    Thank you, that did solve my problem!

  5. #4
    Join Date
    May 2010
    Location
    Rousse, Bulgaria
    Posts
    25
    Thanks
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [QTcpSocket] disconnectFromHost in a class Destructor

    Just to add up, there's one more way to accompish this, one can reimplement QWidget::closeEvent(QCloseEvent*) and use that. Also works and I think it's the better way to do it.

Similar Threads

  1. Please Help, QTcpSocket class use
    By aash_89 in forum Qt Programming
    Replies: 6
    Last Post: 13th July 2010, 03:26
  2. Replies: 5
    Last Post: 20th April 2010, 17:48
  3. DisconnectFromHost generate a runtime error
    By Darthspawn in forum Qt Programming
    Replies: 0
    Last Post: 11th March 2010, 17:35
  4. QTcpSocket as class member of QThread Issue
    By zyangxue in forum Qt Programming
    Replies: 4
    Last Post: 12th December 2009, 06:42
  5. Destructor in QT
    By hgedek in forum Newbie
    Replies: 1
    Last Post: 18th September 2007, 10:54

Tags for this Thread

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.