0

When you close console application in Windows 10, in the following code, Boo object is not being destructed even it is a local variable. I tried to catch interrupts but it didnt worked. I tried to connect to aboutToQuit signal , it didn't worked as well. Is there any way to destruct an object just before quit?

class Boo {
public:
    ~Boo() {
        std::cout << "i'm dying" << std::endl;
    }
};

void handle_quit() {
    std::cout << "I quit" << std::endl;
}


int main(int argc, char *argv[])
{
    Boo test;

    QCoreApplication app(argc, argv);

    QTimer::singleShot(0, &app, [&]() {

        std::cout << "hello world qt";
    });

    QObject::connect(&app, &QCoreApplication::aboutToQuit, [&]() {
        handle_quit();
    });

    return app.exec();
}
Erhan
  • 55
  • 5
  • 1.) Don't worry. You don't lose memory or anything. 2.) How did you quit the application? The way it's written, it will just keep running. 3.) The `aboutToQuit` signal is emitted when the application itself decides to quit. That does not happen, see 2. Try calling the slot `QCoreApplication::quit` e.g. by a timer and see what happens. – Friedrich Jan 30 '23 at 13:32
  • @Friedrich, I quit by closing cmd window. – Erhan Jan 30 '23 at 13:42
  • 1
    And where would you expect the output to show up? – Friedrich Jan 30 '23 at 13:43
  • @Friedrich, ouput was symbolic. Accually I am tring to close a camera device. I am putting breakpoint on debug mode on VS. – Erhan Jan 30 '23 at 13:52
  • And the camera device does not get closed? That's a completely different problem and has nothing to do with stack variables, does it? The way I see it, you either quit your application properly or you handle OS signals. – Friedrich Jan 30 '23 at 14:01
  • @Friedrich No. I want to trigger a function just before the application close. In C++, the best way to do it is using RAII. And this small application proves that RAII is not working in QT environtment,. I am curious how they ruined the RAII. Is it VS compiler problem? Similar problem is here: https://stackoverflow.com/questions/57395491/how-to-run-my-destructor-before-close-the-qt-console-application/75284621#75284621 – Erhan Jan 30 '23 at 14:12
  • "And this small application proves that RAII is not working in QT environtment" Neither does it prove anything nor is it true. Please _read_ the link you posted yourself. It's explained there. – Friedrich Jan 30 '23 at 14:24

1 Answers1

0

Here is the answer:

If you close the concole application by closing cmd window, the application gets SIGBREAK interrupt. In that case, you need to destruct everthing in 5 seconds. After 5 second application is being terminated forcefully by the system. Since my stack is not being destructed in that case, I moved it to heap and destructed it as soon as I got SIGBREAK.

class Boo {
    ~Boo () {
        //do destruction job.. 
    }
}

std::shared_ptr<Boo> test;

void SigInt_Handler(int) {
    //destruct in 5 second...
    test.reset();
}

int main(int argc, char *argv[])
{
    test  = make_shared<Boo>();
    
    signal(SIGBREAK , &SigInt_Handler);

    QCoreApplication app(argc, argv);

    QTimer::singleShot(0, &app, [&]() {

        std::cout << "hello world qt";
    });

    return app.exec();
}
Erhan
  • 55
  • 5