Igor said:
I couldn't reproduce this crash (Boost 1.49, MSVC10, Win7), maybe
because I don't have any trafic on COM1, but any way -- asio objects
are not thread-safe, so it might be unsafe to call cancel() this way.
Instead, try using the following: ioService.post([&]
{serialPort.cancel();}); - and see if it helps.

It definitely helps; it crashed 0 out of 10 times, which is enough for me (it crashed every time before, even without traffic). I didn't know asio objects weren't thread-safe.
I need to use them in a multithreaded environment; I have a reader and a writer thread. I suppose that wrapping every call to the serial_port object inside a post message would avoid any trouble but might be too slow. Am I right? Are there some general guidelines I should follow when using asio objects in multithreaded environments?

Richard said:
Are you using Windows XP? If you are it is due to Win32 API CancelIo can only cancel IO requests from the same thread as the CancelIO request itself. Vista and later has CancelIoEx which can cancel IO requests from any thread. As I remember Boost Asio Serial checks whether CancelIoEx is supported and if not will throw an exception if it determines the CancelIo request is not on the same thread as the outstanding IO request.

That makes a lot of sense, since I am indeed using XP. It also explains why using post solves the issue. Anyway, if Boost performs that check, the Runtime Check failure shouldn't happen. Could this be a bug?

--

Darío Eduardo Ramos
Meditech S.A. www.meditech.com.ar
(+54) 01147603300, Interno 31

Av. Julio A. Roca 3456

Florida Oeste, Bs.As. Argentina