Boost logo

Boost Users :

Subject: [Boost-users] SIGSEGV when removing Ethernet/Serial Port cables
From: Ulf Samuelsson (boost-user_at_[hidden])
Date: 2013-10-30 13:12:46


Have a boost enable application which communicated over
USB serial ports and UDP

If the cables are removed, the library fires off SIGSEGV signals.

I am trying to catch exceptions but neither
     } catch (boost::system::system_error &e) {
     } catch (std::exception &e) {
seems to work, and a SIGSEGV signal is sent when the

         port->get_io_service().reset();

statement is executed.

How can I handle the problem, that the USB serial cable is removed?

I know I can test for the existence of /dev/ttyUSBx in the file system
or /sys/class/tty/ttyUSBx"
I know I can write a SIGSEGV handler, but would like to know if there is
any exception I can catch instead.

Have the same problem when the Ethernet Cable is removed. How handle this?

// Serial Port Read.

// Reads a character or times out
bool SerialPort::read_char(unsigned char& val) {
     val = c = '\0';
     try {
// After a timeout & cancel it seems we need
// to do a reset for subsequent reads to work.
         port->get_io_service().reset();

// Asynchronously read 1 character.
         async_read(*port, buffer(&c, 1),
                 boost::bind(&SerialPort::read_complete, this,
placeholders::error, placeholders::bytes_transferred));
// Setup a deadline time to implement our timeout.
timer->expires_from_now(boost::posix_time::milliseconds(1));
         timer->async_wait(boost::bind(&SerialPort::time_out, this,
placeholders::error));
// This will block until a character is read
// or until the it is cancelled.
         port->get_io_service().run();

         if (!read_error) {
             val = c;
         } else {
             val = 0; // Timeout
         }
     } catch (boost::system::system_error &e) {
         val = 2;
         DebugPort.WriteLine("Serial Port System Error", RED);
         return false;
     } catch (std::exception &e) {
         val = 1;
         DebugPort.WriteLine("Serial Port General Error", RED);
         return false;
     }
     return !read_error;
}

// Network port open

bool UdpClient::Open(void)
{
         udp::resolver resolver(io_service);
         udp::resolver::query query(udp::v4(), ipAddress, Port);
         try {
             Endpoint = *resolver.resolve(query);
         } catch (boost::system::system_error &e) {
             // We might not have a network connection
             return false;
         }
         if (!CreateSocket()) {
             return false;
         }
         if (!OpenSocket()) {
             return false;
         }
         init = true;
         return true;
}

Best Regards
Ulf Samuelsson


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net