Results 1 to 6 of 6

Thread: Segmentation fault when try to send message

  1. #1
    Join Date
    Aug 2007
    Posts
    64
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Segmentation fault when try to send message

    I'm having a seg fault problem that I don't know how solve it.
    I have a client program with a lot of QSpinBox, some of them was promoted to a custom class SpinView. When someone change the value in SpinView it automatically call a function of my SocketManipulation class to send the change to gateway application, but when the SocketManipulation try to send it returns a seg fault.
    I've compared the memory address of SocketManipulation instance and this is the same, stopped the read of socket cause I was thinking they was accessing the socket at the same time, checked the message passed and the size of it but the same problem is happening!
    I think the problem isn't in SocketManipulation function cause I called it manually and it works!
    What could be it?
    These are the codes:

    Qt Code:
    1. #include "SocketManipulator.h"
    2. #include <QStringList>
    3.  
    4. #include <iostream>
    5.  
    6. using namespace std;
    7.  
    8. SocketManipulator::SocketManipulator(QObject *parent, QString h, int p): QThread(parent){
    9. hostName=h;
    10. porta=p;
    11. piorCasoLeitura=45;
    12. piorCasoEscrita=12;
    13. socket = new QTcpSocket(NULL);
    14. socket->setReadBufferSize(piorCasoLeitura);
    15. socket->connectToHost(hostName, porta);
    16. start();
    17. }
    18.  
    19. SocketManipulator::~SocketManipulator(void){
    20. if(socket!=NULL)
    21. delete(socket);
    22. }
    23.  
    24. QList<int> SocketManipulator:: getdIn(){return dIn;}
    25. QList<int> SocketManipulator:: getdOut(){return dOut;}
    26. QList<int> SocketManipulator:: getaIn(){return aIn;}
    27. QList<int> SocketManipulator:: getaOut(){return aOut;}
    28. QList<int> SocketManipulator:: getaTypes(){return aTypes;}
    29.  
    30. void SocketManipulator::requestWrite(QString msg){
    31. socket->write(msg.toStdString().c_str(),50/*piorCasoEscrita*/);
    32. }
    33.  
    34. void SocketManipulator::run(void){
    35. while(true){
    36. socket->waitForReadyRead();
    37. read();
    38. // requestWrite("Idiota!\n");
    39. }
    40. //sign:1,1
    41. //unsign:1,1
    42. }
    43.  
    44. void SocketManipulator::read(void){
    45. QString msg;
    46. if(socket->bytesAvailable() >= piorCasoLeitura)
    47. msg=socket->read(piorCasoLeitura);
    48. cout<<"Mensagem recebida: "<<msg.toStdString();
    49. processMsg(msg);
    50. }
    51.  
    52. void SocketManipulator::processMsg(QString msg){
    53. QStringList lista;
    54. QString type;
    55. QList<int> values;
    56. values.clear();
    57. lista = msg.split(":");
    58. type=lista.value(0);
    59. lista=lista.value(1).split(";");
    60. for(int cont=0; cont<lista.size(); cont++)
    61. values.append(lista.value(cont).toInt());
    62. if(type=="aOut"){
    63. aOut=values;
    64. emit receivedAOut();
    65. }
    66. if(type=="aIn"){
    67. aIn=values;
    68. emit receivedAIn();
    69. }
    70. if(type=="dIn"){
    71. dIn=values;
    72. emit receivedDIn();
    73. }
    74. if(type=="dOut"){
    75. dOut=values;
    76. emit receivedDOut();
    77. }
    78. if(type=="aTypes"){
    79. aTypes=values;
    80. emit receivedATypes();
    81. }
    82. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "SpinView.h"
    2.  
    3. SpinView::SpinView(QWidget *parent):QSpinBox(parent){
    4. connect(this,SIGNAL(valueChanged(int)),this,SLOT(setOutValue()));
    5. }
    6.  
    7. void SpinView::setSocket(SocketManipulator *s){
    8. socket=s;
    9. }
    10.  
    11. QString SpinView::createMessage(){
    12. QString message;
    13. QString aux;
    14. QStringList lista=this->objectName().split("Spin");
    15. if(lista.value(0)=="aOut")
    16. message="setAOut:";
    17. else
    18. message="setDOut:";
    19. message+=aux.setNum(this->value());
    20. message+=";";
    21. message+=lista.value(1);
    22. return(message);
    23. }
    24.  
    25. void SpinView::setOutValue(void){
    26. socket->requestWrite(createMessage());
    27. }
    To copy to clipboard, switch view to plain text mode 

    Another thing I forgot to tell you. When I run the application this message is shown:

    QObject: Cannot create children for a parent that is in a different thread.
    (Parent is QTcpSocket(0x81511b0), parent's thread is QThread(0x807b390), current thread is SocketManipulator(0x8150ee8)
    QObject: Cannot create children for a parent that is in a different thread.
    (Parent is QTcpSocket(0x81511b0), parent's thread is QThread(0x807b390), current thread is SocketManipulator(0x8150ee8)
    Perhaps this is the problem but I don't know how to solve it to!

  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: Segmentation fault when try to send message

    What do you need threads for? QTcpSocket works asynchronously unless you use those blocking waitXXX() methods.

    Your problem is that the constructor of SocketManipulator is executed in the thread that creates it. Later on QThread starts a new thread when you call start() and the run() function gets executed in the newly started thread. Currently you create the socket in one thread (in constructor) and use it in different thread (in run). This is a no no. You should create the socket in run().
    J-P Nurmi

  3. #3
    Join Date
    Aug 2007
    Posts
    64
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Segmentation fault when try to send message

    I did what you said. The warning messages aren't shown right now but the seg fault still happens!!

  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: Segmentation fault when try to send message

    Why don't you answer my first question? It's one of the most common mistakes to use threads with Qt sockets even if it's completely unnecessary in simple cases. Qt sockets are asynchronous. They don't block the GUI. Using threads just makes things a lot more error-prone as you have already noticed.

    The next problem is that you use direct calls to communicate between two threads. Your thread class should enter to an event loop, and then you should use signals and slots or custom events to deliver the message to be written to the correct thread.
    J-P Nurmi

  5. #5
    Join Date
    Aug 2007
    Posts
    64
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Segmentation fault when try to send message

    This program is a simple client, it receives a lot of data and sometimes it should messages to set in gateway what type of message it will receive.

    I changed the class of socket, now it isn't inheriting QThread, but...
    the seg fault still happen!

    I was looking that others parts of the program can send messages well, just when I set in user interface the SpinView it crashes in the function used to send.

  6. #6
    Join Date
    Aug 2007
    Posts
    64
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Segmentation fault when try to send message

    I've reimplemented SpinView and it's sending messages now!

    Thanks

Similar Threads

  1. segmentation fault
    By uchennaanyanwu in forum Newbie
    Replies: 3
    Last Post: 31st July 2008, 16:52
  2. Process aborted. Segmentation fault
    By Pragya in forum Qt Programming
    Replies: 3
    Last Post: 30th May 2007, 08:12
  3. Replies: 2
    Last Post: 19th May 2007, 18:25
  4. Segmentation fault running any QT4 executables
    By jellis in forum Installation and Deployment
    Replies: 7
    Last Post: 19th May 2007, 16:35
  5. Icons missing => segmentation fault
    By antonio.r.tome in forum Qt Programming
    Replies: 4
    Last Post: 8th March 2006, 16:30

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.