I've read several questions here on Stack Overflow, Microsoft docs, and cplusplus.com. Some claim that exit() terminates the program normally, just as a return 0; would from main. Others claim that exit() doesn't call all the destructors, etc. So I was wondering if someone could help.
edit: As someone asked, I added some blocks of code for which I would like to terminate the program. I use C++20
HKEY newKey;
RegOpenKey(HKEY_CURRENT_USER, R"(Software\Microsoft\Windows\CurrentVersion\Run)", &newKey);
LONG result = RegSetValueEx(newKey, value, 0, REG_SZ, (LPBYTE)filePath, lstrlen(filePath));
RegCloseKey(newKey);
if(result != ERROR_SUCCESS){
MessageBox(nullptr, "Could not add to startup", "Error", MB_ICONERROR);
exit(1);
}
int i = line.find(':');
if(i == std::string::npos){
MessageBox(nullptr, "File is incorrectly formatted", "Error", MB_ICONERROR);
exit(1);
}
info.open(infoPath);
if(info.fail()){
MessageBox(nullptr, "info.txt did not open", "Error", MB_ICONERROR);
exit(1);
}
I link the posts I've read about this:
How to end C++ code
https://cplusplus.com/forum/beginner/4589/
How to exit program execution in C++?
https://learn.microsoft.com/en-us/cpp/cpp/program-termination?view=msvc-170
https://cplusplus.com/reference/cstdlib/exit/
Thanks in advance
Some claim that exit() terminates the program normally, just as a return 0; would from main
std::exit gets called anyway, as part of standard application lifecycle. Quote from CPPReference:
Returning from the main function, either by a return statement or by reaching the end of the function performs the normal function termination (calls the destructors of the variables with automatic storage durations) and then executes std::exit, passing the argument of the return statement (or 0 if implicit return was used) as exit_code.
Others claim that exit() doesn't call all the destructors, etc
It's true, but it doesn't cancel the first statement. I.e. if destructors of you classes don't have any side effects which can survive the program itself, it doesn't matter whether the local variables were destroyed properly or not, since entire memory of your process is freed after program termination. std::exit however calls destructors of objects with static and thread local storage duration:
The destructors of objects with thread local storage duration that are associated with the current thread, the destructors of objects with static storage duration, and the functions registered with std::atexit are executed concurrently
It is completely unreasonable to assume a program is going to properly fall all the way out of main() during any form of error handling. In the case of a we should not / can not go any further, professional programs call exit() all the time.
However, if you have objects that NEED to be cleaned up, then you need to implement an atexit handler. See:
https://en.cppreference.com/w/cpp/utility/program/atexit
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With