Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to kill an application at main

How to kill an application at main

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 5 Posters 7.8k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    zeroptr
    wrote on last edited by
    #1

    Hi All

    I have a main func like this..

    int main(int argc, char *argv[])
    {
    bool NoUserLogon = false;
    cn::obj();

    QApplication a(argc, argv);
    
    
    frmMAIN mfrmMAIN;
    

    cn::_obj->AssignParameters();

    if (!cn::db().open())
    {
        QMessageBox(QMessageBox::Critical, "Dikkat !", "Veri Tabanı Bağlantısı Yapılamadı Program ayarlarını kontrol ediniz veya Sistem Yöneticiniz ile görüşünüz!", QMessageBox::Ok).exec();
        mfrmMAIN.mfrmDB_Settings.exec();
    }
    st_T_USR *The_User_Info = new st_T_USR;
    
    frmLOGIN *mfrmLOGIN = new frmLOGIN;
    
    mfrmLOGIN->Get_The_User(The_User_Info);
    
    qDebug() << The_User_Info->Name_Full;
    qDebug() << The_User_Info->Logon;
    qDebug() << The_User_Info->Password;
    qDebug() << QString::number(The_User_Info->Admin);
    qDebug() << QString::number(The_User_Info->Operator);
    
    mfrmLOGIN->setWindowFlags(Qt::Dialog
                              | Qt::CustomizeWindowHint);
    if (mfrmLOGIN->exec() == QMessageBox::Accepted) {
        QMessageBox(QMessageBox::Critical, "HEyo !", "Accepted", QMessageBox::Ok).exec();
    
    }
    else
    {
        NoUserLogon = true;
        qDebug() << "No User Logon True";
        mfrmMAIN.close();
        // I WANT TO KILL THIS APP HERE
    }
    
    delete mfrmLOGIN;
    mfrmMAIN.showMaximized();
    
    return a.exec();
    

    }

    I tried
    qApp.exit();
    QApplication::quit();

    but nothing works?

    Linux Mint 20.04 64 Bit QT6.0.1

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #2

      Neither exit nor quit will work since you're not in an event loop yet (a.exec()).
      You can simply use return 0; to exit main. Just cleanup before you do

      1 Reply Last reply
      0
      • D Offline
        D Offline
        deleted605
        wrote on last edited by
        #3

        You may also try std c++ exit() function. It terminates the application immediately. It also starts a cleaning procedure for the allocated memory (as I know).

        http://www.cplusplus.com/reference/cstdlib/exit/

        Also you may have a look at abort();
        http://www.cplusplus.com/reference/cstdlib/abort/

        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @canavaroski these are very harsh ways to exit an app. They are not intended for regular exiting. They have also some repercussions as to how the OS interprets app quitting in such way.
          Unless it's some extraordinary error handling the easiest and cleanest way is to just return from main.

          1 Reply Last reply
          0
          • Z Offline
            Z Offline
            zeroptr
            wrote on last edited by
            #5

            Hi All,

            return 0 seems to work...

            in my code there is a poiter to a struct.. I don't delete it on application lifetime...
            does that variable cleans from memory when the apllication end automatically?

            Thanks..

            Linux Mint 20.04 64 Bit QT6.0.1

            mrjjM J 2 Replies Last reply
            0
            • Z zeroptr

              Hi All,

              return 0 seems to work...

              in my code there is a poiter to a struct.. I don't delete it on application lifetime...
              does that variable cleans from memory when the apllication end automatically?

              Thanks..

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @zeroptr
              Hi
              if you do anything like
              MyStruct *s = new MyStruct;
              then no, it wont be cleaned.

              1 Reply Last reply
              0
              • Chris KawaC Offline
                Chris KawaC Offline
                Chris Kawa
                Lifetime Qt Champion
                wrote on last edited by Chris Kawa
                #7

                Well, when the process terminates the OS will clean up, but you're destructors won't get called and you're missing out on opportunity to free up any external resources you might have grabbed (like initiated connections, opened files, stored settings etc.). Unless you're absolutely sure there's nothing important being skipped it's a good practice to free up anything you might have allocated.
                Also, if you don't clean up, every leak detection mechanism out there will freak out if you decide to use one someday.

                1 Reply Last reply
                0
                • Z Offline
                  Z Offline
                  zeroptr
                  wrote on last edited by
                  #8

                  Thanks All,

                  I love this community...

                  Linux Mint 20.04 64 Bit QT6.0.1

                  mrjjM 1 Reply Last reply
                  0
                  • Z zeroptr

                    Thanks All,

                    I love this community...

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @zeroptr
                    That is nice!
                    and Qt is not bad either ;)

                    1 Reply Last reply
                    0
                    • Z zeroptr

                      Hi All,

                      return 0 seems to work...

                      in my code there is a poiter to a struct.. I don't delete it on application lifetime...
                      does that variable cleans from memory when the apllication end automatically?

                      Thanks..

                      J Offline
                      J Offline
                      Jakob
                      wrote on last edited by
                      #10

                      @zeroptr No it doesn't clean up, unless you consistently use RAII-techniques, which are easily implemented using smart pointers, see https://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization and http://en.cppreference.com/w/cpp/memory/unique_ptr

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved