error_category mismatch in asio when used across dlls

  asio, boost-asio, c++, dll, error-handling

I have a problem with handling asio::error_code values when they are received from another dll or executable. For instance, I may run an asynchronous operation with a handler:

socket.async_receive([](const asio::error_code& errorCode)
{
    if (errorCode == asio::error::operation_aborted)
       return;
}

If the handler is created within a dll, and an errorCode is passed from another dll or executable, operator== would return false, since their error_category values differ. For example:

(gdb) info sym 0x55f1c0
asio::system_category()::instance in section .data of C:devbuildsUWCASdkDebug-MinGW-w64SandboxServerL410Fbinsb.ServerL410F.exe
(gdb) info sym 0x6f0420d0
asio::system_category()::instance in section .data of C:devbuildsUWCASdkDebug-MinGW-w64SandboxServerL410Fbinlibproviders.protobuf.client_d.dll

Clearly, these two instances are defined in different units and I basically have ODR violation.

instance is defined within asio/impl/error_code.ipp:

const error_category& system_category()
{
  static detail::system_category instance;
  return instance;
}

ASIO documentation doesn’t mention anything about error_cateegory when handling errors – it only specifies enum values, so using errorCode.value == asio::error::operation_aborted seems to be a valid workaround. But still this seems ugly.

  • Is it somehow possible to compare errors across dlls as intended (with overloaded operator==)?
  • Is it normal or expected to compare only errorCode.value() against enums?

Source: Windows Questions C++

LEAVE A COMMENT