Fixing compiler warning C4834 - discarding return value of std::async function

155 views Asked by At

I am fixing warnings in C++ codebase. There is this function that uses std::async, but the return std::future object is not captured, so the compiler is raising C4834 warning. The function is as below:

LONG WINAPI VexHandler(PEXCEPTION_POINTERS pExceptionPtrs)
{
    if (pExceptionPtrs->ExceptionRecord->ExceptionCode == EXCEPTION_STACK_OVERFLOW)
    {
        std::async(WriteMiniDumpWithActualJniContext, pExceptionPtrs);
        quick_exit(1);
    }
    else if (pExceptionPtrs->ExceptionRecord->ExceptionCode == EXCEPTION_BREAKPOINT)
    {
        WINDUMP_IMPLEMENTATION->WriteMiniDump(pExceptionPtrs);

        string exceptionInformation = GetExceptionsInformation(pExceptionPtrs);
        throw std::runtime_error(exceptionInformation);
    }

    return EXCEPTION_EXECUTE_HANDLER;
}

To fix this warning I have captured the return from std::async and called wait() on that.

...
if (pExceptionPtrs->ExceptionRecord->ExceptionCode == EXCEPTION_STACK_OVERFLOW)
{
    auto ftr = std::async(WriteMiniDumpWithActualJniContext, pExceptionPtrs);
    ftr.wait();
    quick_exit(1);
}
...

Which way is better to fix this warning as per C++17 standard?

Alternatively I think I could have typecasted the return to void as below:

...
if (pExceptionPtrs->ExceptionRecord->ExceptionCode == EXCEPTION_STACK_OVERFLOW)
{
    static_cast<void>(std::async(WriteMiniDumpWithActualJniContext, pExceptionPtrs));
    quick_exit(1);
}
...
0

There are 0 answers