0

I am using qt for my GUI but I coded my original server in c++ (CLion). When I run my whole project in qt, I read this error on the console: 'The program has unexpectedly finished' 'The program was ended forcefully'. Here is the main function:

int server_main::begin() {

    N = server_main::read_configuration();

    char database_name[20];
    Database* db;
    sprintf(database_name, "sniff.db");
    if(!exist_db(database_name)){
        printf("File NOT found!\n");
        db = new Database(false);
    }
    else {
        printf("File found!\n");
        db = new Database(true);
    }

    Analyzer* analyzer = new Analyzer(N, db, pos);
    analyzer->init();

    cout << "CREATING SERVER..." << endl;
    Server* server = new Server(PORT, N, analyzer);
    int err = server->init();
    cout << "Server successfully created!" << endl;

    if (err != 0) {
        cerr << strerror(err) << endl;
        exit(err);
    }

    thread server_thread(start,server);
    server_thread.join();


    while(1){
        analyzer->update_view();
    }

    return 0;}

The problem is in the following two lines of code, when I run my server in background:

thread server_thread(start,server); server_thread.join();

Does anyone know how to fix the problem? Thanks in advance

  • 1
    If you do `server_thread.join();`, then it's not going to run in the background. How are you exiting from the while loop? – Matthieu Brucher Dec 14 '18 at 13:32
  • loop is not executed, I have put a comment and I receive the same errors – Napoleone Capasso Dec 14 '18 at 13:35
  • I'm guessing the real problem lies in the `start` function -- can you show the code? Also, run the program under a debugger and get a stacktrace at the point of the crash. – G.M. Dec 14 '18 at 13:43
  • `while(1){ analyzer->update_view(); }` also is not going to work with `Qt`remember that Qt paints to the screen when it returns to process the event loop. If you don't let it return the screen will not update and the application will appear frozen. There is a way around the update problem but its better to fix the design to be event driven. – drescherjm Dec 14 '18 at 14:00
  • The bug causing `The program has unexpectedly finished` is likely unrelated to the code shown. However I can say the threading code needs to be totally redesigned. It will not work the way you are trying. The threads can not be a local variable to this function. You also need to get rid of the while(1) loop entirely. It may be simpler to use one of the `Qt` threading techniques especially if you want to display the result of data generated from a thread in real time. – drescherjm Dec 14 '18 at 16:24
  • 1
    There is a lot of good advice in the answers to this question: https://stackoverflow.com/questions/40284850/qthread-vs-stdthread I recommend reading all 3 answers. – drescherjm Dec 14 '18 at 16:33

0 Answers0