Results 1 to 6 of 6

Thread: Crash of the Server Application

  1. #1
    Join Date
    Nov 2012
    Posts
    232
    Thanks
    118
    Thanked 18 Times in 10 Posts
    Platforms
    Windows Android

    Default Crash of the Server Application

    Hi,

    When I connect with the Server Application I receive the Crash Error on the line:

    Qt Code:
    1. if( !socket->setSocketDescriptor( m_descriptor ) )
    To copy to clipboard, switch view to plain text mode 

    Please run the project "ServerTimer" than run "ViewerServerTimer" and click the button "Connect"

    ServerTimer.pro
    Qt Code:
    1. QT += core
    2. QT += network
    3. QT -= gui
    4.  
    5. TARGET = ServerTimer
    6. CONFIG += console
    7. CONFIG -= app_bundle
    8.  
    9. TEMPLATE = app
    10.  
    11.  
    12. SOURCES += main.cpp \
    13. server.cpp \
    14. serverthread.cpp
    15.  
    16. HEADERS += \
    17. server.h \
    18. serverthread.h
    To copy to clipboard, switch view to plain text mode 

    server.h
    Qt Code:
    1. #ifndef SERVER_H
    2. #define SERVER_H
    3.  
    4. #include <QTcpServer>
    5. #include "serverthread.h"
    6.  
    7. class Server : public QTcpServer
    8. {
    9. public:
    10. Server();
    11.  
    12. protected:
    13. void incomingConnection(int descriptor);
    14.  
    15. private:
    16. ServerThread *thread;
    17. };
    18.  
    19. #endif // SERVER_H
    To copy to clipboard, switch view to plain text mode 

    server.cpp
    Qt Code:
    1. #include "server.h"
    2.  
    3. Server::Server() : QTcpServer()
    4. {
    5. }
    6.  
    7. void Server::incomingConnection( int descriptor )
    8. {
    9. thread = new ServerThread( descriptor, this );
    10.  
    11. connect( thread, SIGNAL(finished()), thread, SLOT(deleteLater()) );
    12. thread->start();
    13. }
    To copy to clipboard, switch view to plain text mode 

    serverthread.h
    Qt Code:
    1. #ifndef SERVERTHREAD_H
    2. #define SERVERTHREAD_H
    3.  
    4. #include <QThread>
    5. #include <QTimer>
    6. #include <QTcpSocket>
    7.  
    8. class ServerThread : public QThread
    9. {
    10. public:
    11. ServerThread( int descriptor, QObject *parent );
    12.  
    13. void run();
    14.  
    15. public slots:
    16. void sendData();
    17.  
    18. private:
    19. int m_descriptor;
    20.  
    21. private:
    22. QTimer *timer;
    23. QTcpSocket *socket;
    24. };
    25.  
    26. #endif // SERVERTHREAD_H
    To copy to clipboard, switch view to plain text mode 

    serverthread.cpp
    Qt Code:
    1. #include "serverthread.h"
    2.  
    3. ServerThread::ServerThread( int descriptor, QObject *parent ) : QThread( parent )
    4. {
    5. m_descriptor = descriptor;
    6. }
    7.  
    8. void ServerThread::run()
    9. {
    10. if( !socket->setSocketDescriptor( m_descriptor ) )
    11. {
    12. qDebug( "Socket error!" );
    13. return;
    14. }
    15.  
    16. timer = new QTimer;
    17. connect(timer, SIGNAL(timeout()), this, SLOT(sendData()));
    18. timer->start(1000);
    19. }
    20.  
    21. void ServerThread::sendData() {
    22. //socket->write( "data" );
    23. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include <QCoreApplication>
    2. #include "server.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QCoreApplication a(argc, argv);
    7.  
    8. Server server;
    9. if( !server.listen( QHostAddress::Any, 1234 ) )
    10. {
    11. qCritical( "Cannot listen to port 1234." );
    12. return 1;
    13. }
    14.  
    15. return a.exec();
    16. }
    To copy to clipboard, switch view to plain text mode 

    Thank you
    Attached Files Attached Files

  2. #2
    Join Date
    Nov 2012
    Posts
    232
    Thanks
    118
    Thanked 18 Times in 10 Posts
    Platforms
    Windows Android

    Default Re: Crash of the Server Application

    Please, download and run my application. It's very important for me. Please, help me. I don't know where the mistake.

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Crash of the Server Application

    The member variable 'socket' is never initialised to point a QTcpSocket allocated in free store. It is an invalid pointer when you try to use it in run().


    Can I suggest you develop the habit of always initialising pointer member variables to 0 or a valid pointer in the class constructor initialisation list or body. A zero value in a pointer is immediately obvious on your debugger where the random value that might otherwise be there is not.

    Something like:
    Qt Code:
    1. ServerThread::ServerThread( int descriptor, QObject *parent ):
    2. QThread( parent ), m_descriptor( descriptor ), timer( 0 ), socket( 0 )
    3. {
    4. }
    To copy to clipboard, switch view to plain text mode 
    or
    Qt Code:
    1. ServerThread::ServerThread( int descriptor, QObject *parent ):
    2. QThread( parent )
    3. {
    4. m_descriptor = descriptor;
    5. timer = 0;
    6. socket = 0;
    7. }
    To copy to clipboard, switch view to plain text mode 

    You do not need threads in this example.
    Last edited by ChrisW67; 2nd September 2013 at 00:00.

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

    8Observer8 (3rd September 2013)

  5. #4
    Join Date
    Nov 2012
    Posts
    232
    Thanks
    118
    Thanked 18 Times in 10 Posts
    Platforms
    Windows Android

    Default Re: Crash of the Server Application

    Thank you!

    I wrote it:
    Qt Code:
    1. void ServerThread::run()
    2. {
    3. socket = new QTcpSocket;
    4. ....
    5. }
    To copy to clipboard, switch view to plain text mode 

    You do not need threads in this example.
    I will have the many clients.

    Output:

    QObject: Cannot create children for a parent that is in a different thread.
    (Parent is QThread(0x547258), parent's thread is QThread(0x5416a8), current thre
    ad is QThread(0x547258)
    Object::connect: No such slot QThread::sendData() in ../ServerTimer/serverthread
    .cpp:20

  6. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Crash of the Server Application

    I will have the many clients.
    That still does not require threads. If the typical response to a connection is short then adding threads probably complicate matters for no benefit.

    QObject: Cannot create children for a parent that is in a different thread.
    (Parent is QThread(0x547258), parent's thread is QThread(0x5416a8), current thread is QThread(0x547258)
    Object::connect: No such slot QThread::sendData() in ../ServerTimer/serverthread.cpp:20
    This is one of those complications.

    Consult the Fortune Server Example and the Threaded Fortune Server examples in Assistant.

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

    8Observer8 (3rd September 2013)

  8. #6
    Join Date
    Nov 2012
    Posts
    232
    Thanks
    118
    Thanked 18 Times in 10 Posts
    Platforms
    Windows Android

    Default Re: Crash of the Server Application

    Quote Originally Posted by ChrisW67 View Post
    Consult the Fortune Server Example and the Threaded Fortune Server examples in Assistant.
    Thank you. I need to study about threads:
    - You’re doing it wrong…
    - QThreads General Usage

    I found the solution for my task: C++ Qt 70 Advanced Asynchronous QTcpServer with QThreadPool

Similar Threads

  1. how to avoid crash of my application......
    By newb in forum Qt Programming
    Replies: 1
    Last Post: 30th August 2010, 10:09
  2. application crash problem
    By anshul in forum Newbie
    Replies: 3
    Last Post: 25th December 2009, 12:27
  3. Application crash in Vista
    By yj... in forum Installation and Deployment
    Replies: 1
    Last Post: 5th June 2009, 08:18
  4. Weird application crash
    By MarkoSan in forum Qt Programming
    Replies: 10
    Last Post: 20th May 2008, 14:13
  5. Replies: 5
    Last Post: 24th April 2006, 15:42

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.