c++ - Why is a destructor not called when I click the close button on the console? -


i have command line application on windows 7. consists of endless loop. when click on close button of command-line console window, seems killed instantly. however, close sqlite database connection in destructor, , afraid databse might corrupted. if not, i'd able terminate object (maybe writing file, logging, etc.).

can somehow make sure destructor called? happens if close window? there "softer" way close program?

a console application doesn't have message loop you're asked quit windows gives ability register function receive some notifications may need handle. 1 of them closing notification.

first of declare function windows call purpose prototype given handlerroutine:

bool winapi consolehandlerroutine(dword dwctrltype) {     if (ctrl_close_event == dwctrltype) {         return true;     }      return false; } 

now register function, using setcontrolctrlhandler, before start loop:

int main(int argc, char* argv[]) {     if (false == setconsolectrlhandler(consolehandlerroutine, true)) {         // cannot register handler? check getlasterror()     }      while (true)     {         // loop     }      return 0; } 

that's done. can explicitly delete objects want dispose or set flag break infinite loop.

note receive more events (windows shutdown ctrl_shutdown_event, user log-off ctrl_logoff_event, user break ctrl_c_event, closing ctrl_close_event), handle need/want.


Comments

Popular posts from this blog

javascript - Google App Script ContentService downloadAsFile not working -

javascript - Function overwritting -

php - Find a regex to take part of Email -