How to kill an application at main



  • 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?


  • Moderators

    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



  • 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/


  • Moderators

    @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.



  • 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..


  • Qt Champions 2016

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


  • Moderators

    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.



  • Thanks All,

    I love this community...


  • Qt Champions 2016

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



  • @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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.