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;

    QApplication a(argc, argv);
    frmMAIN mfrmMAIN;


    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();
    st_T_USR *The_User_Info = new st_T_USR;
    frmLOGIN *mfrmLOGIN = new frmLOGIN;
    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);
                              | Qt::CustomizeWindowHint);
    if (mfrmLOGIN->exec() == QMessageBox::Accepted) {
        QMessageBox(QMessageBox::Critical, "HEyo !", "Accepted", QMessageBox::Ok).exec();
        NoUserLogon = true;
        qDebug() << "No User Logon True";
    delete mfrmLOGIN;
    return a.exec();


    I tried

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

    Also you may have a look at 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?


  • Qt Champions 2017

    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 2017

    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 and

Log in to reply

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