I am developing a native C++ application on Windows 10 using Visual Studio 2019. In order to perform cleanup activities when my application crashes, I wish to use the RegisterApplicationRecoveryCallback function to register a recovery callback function.
The callback function works as expected in some situations, for example when dereferencing a nullptr.
My problem is that it is not executed when std::abort is called, for example as a result of an unhandled C++ exception.
Has anyone been able to make the RegisterApplicationRecoveryCallback function with std::abort()?
Edit: As Remy Lebeau points out, std::abort() might be interpreted a clean shutdown, but still, with unhandled C++ exceptions, Windows Error Reporting is generating a dump, and I would expect the WER to call the recovery callback when it is registered.
Example code, where I expect "Callback called" to be printed, but it is not:
#include <Windows.h>
#include <cstdio>
#include <stdexcept>
DWORD __stdcall Callback(PVOID)
{
BOOL cancelled = FALSE;
ApplicationRecoveryInProgress(&cancelled);
printf("Callback called");
ApplicationRecoveryFinished(TRUE);
return 0;
}
int main()
{
RegisterApplicationRecoveryCallback(Callback, nullptr, 5000, 0);
throw std::runtime_error("Unhandled exception");
}
Thanks to all who helped answering this post. From the discussion, it appears that the recovery callback registered with RegisterApplicationRecoveryCallback is intentionally not run when std::abort is called. A possible work-around is to call RaiseException from a SIGABRT
handler.
Edit: std::abort may boil down to a call to the __fastfail intrinsic, resulting in a non-continuable exception with exception code 0xC0000409. According to the __fastfail documentation , this tells Windows Error Reporting that minimal in-process actions should be taken in response to the failure. Since the Application Recovery Callback is indeed an in-process action, it is reasonable for Windows Error Reporting to not call the callback when std::abort is called.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.