Results 1 to 4 of 4

Thread: Segfault

  1. #1
    Join Date
    Oct 2007
    Posts
    38
    Thanks
    1

    Default Segfault

    You can only debug what you know is wrong. In this case, with my cursory knowledge of Qt, I know this code should work. (Well I know it doesn't but I don't know why.)

    Problem description: I can start multiple apps and they can connect to one another, but as soon as a connection is terminated all of the apps segfault.

    The problem is most assuredly within here:
    Qt Code:
    1. void MSocketThread::run() {
    2. MSocket mSocket;
    3. if ( 0 != socketDescriptor ) {
    4. if ( !mSocket.setSocketDescriptor(socketDescriptor) ) {
    5. std::cerr << "Failed to set SocketDescriptor." << endl;
    6. }
    7. /* Output who joined the server */
    8. QHostAddress ipaddress = mSocket.peerAddress();
    9. QString ipString(tr("<font color=blue>%1 has joined.</font>").arg(ipaddress.toString()));
    10. emit relayIncomingText(ipString);
    11. }
    12. connect(&mSocket, SIGNAL(connected()),
    13. this, SIGNAL(connectionEstablished()));
    14. connect(&mSocket, SIGNAL(inboundText(QString)),
    15. this, SIGNAL(relayIncomingText(QString)));
    16. connect(this, SIGNAL(sigSend(QString)),
    17. &mSocket, SLOT(send(QString)));
    18. connect(&mSocket, SIGNAL(error()),
    19. this, SIGNAL(quit()));
    20. connect(&mSocket, SIGNAL(disconnected()), this, SLOT(quit()));
    21.  
    22. if ( 0 == socketDescriptor ) {
    23. mSocket.connectToHost(ip, port);
    24. }
    25.  
    26.  
    27. exec(); //Begin event loop.
    28. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Segfault

    Quote Originally Posted by Dumbledore View Post
    Problem description: I can start multiple apps and they can connect to one another, but as soon as a connection is terminated all of the apps segfault.
    There could be lots of reasons. Run the application via debugger, make it crash, and see the backtrace.

    Quote Originally Posted by Dumbledore View Post
    Qt Code:
    1. connect(&mSocket, SIGNAL(connected()),
    2. this, SIGNAL(connectionEstablished()));
    3. connect(&mSocket, SIGNAL(inboundText(QString)),
    4. this, SIGNAL(relayIncomingText(QString)));
    5. connect(this, SIGNAL(sigSend(QString)),
    6. &mSocket, SLOT(send(QString)));
    7. connect(&mSocket, SIGNAL(error()),
    8. this, SIGNAL(quit()));
    9. connect(&mSocket, SIGNAL(disconnected()), this, SLOT(quit()));
    To copy to clipboard, switch view to plain text mode 
    Be extremely careful with such connections. "mSocket" lives in different thread than "this", which causes all the connections to be queued, which means that the slots get also called in different thread than MSocketThread::run(). They get called in thread where MSocketThread, the receiver, lives in. This can be avoided by explicitly passing Qt:irectConnection where needed.
    J-P Nurmi

  3. #3
    Join Date
    Oct 2007
    Posts
    38
    Thanks
    1

    Default Re: Segfault

    I don't know what you are talking about. My understanding is QThread::run is its own thread, and whatever I do within that thread is part of that thread... (Creating MSocket on the stack would mean that it runs within that thread.)

    Is there half decent documentation out there on this? I can't make sense of the QT4 documentation on threads and the book GUI programming with QT doesn't do justice to threading.

    One negative I have noticed about QT is there's a lack of code examples.

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Segfault

    Quote Originally Posted by Dumbledore View Post
    I don't know what you are talking about. My understanding is QThread::run is its own thread, and whatever I do within that thread is part of that thread... (Creating MSocket on the stack would mean that it runs within that thread.)

    Is there half decent documentation out there on this? I can't make sense of the QT4 documentation on threads and the book GUI programming with QT doesn't do justice to threading.
    Yes, but since the QThread object itself lives in another thread than the MSocket instance, signal slot connections between them get queued by default and therefore corresponding slots might end up being called in different thread than you expect. It all depends what your slots actually do. Check out Brad Hughes' multithreading presentation from Trolltech DevDays 2007: http://chaos.troll.no/~ahanssen/devd...-Threading.pdf, starting from page 33. Also, I noticed usage of both SIGNAL(quit()) and SLOT(quit()), is this intentional?

    One negative I have noticed about QT is there's a lack of code examples.
    Really? http://doc.trolltech.com/latest/examples.html
    J-P Nurmi

Similar Threads

  1. Qt 4.3.0beta svg text segfault
    By mr.costa in forum Qt Programming
    Replies: 1
    Last Post: 26th April 2007, 08:04
  2. Replies: 1
    Last Post: 19th January 2007, 11:27
  3. segfault
    By conexion2000 in forum Qt Programming
    Replies: 1
    Last Post: 31st May 2006, 12:34
  4. Why does setTextColor() cause a segfault?
    By johnny_sparx in forum Qt Programming
    Replies: 1
    Last Post: 1st April 2006, 16:58
  5. Replies: 10
    Last Post: 10th February 2006, 00:15

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.