Search code examples
csegmentation-faultembeddedposix-select

select() seems to segfault / kill


I am trying to debug an embedded application that uses select() to implement communication timeouts. It is multithreaded using pthreads.

The problem I have is that the application seems to crash on the select() call with either a segmentation fault or "Killed". I tried putting a mutex around the call to no avail.

What could cause select() to crash an application? Or am I in completely the wrong direction?

The call to select() looks like this:

fd_set rfds;
struct timeval tv;
int retval, timeout, timeout_usec = 0;

FD_ZERO(&rfds);
FD_SET(fd_port, &rfds);

if (use_timeout) {
    timeout = settings_get_int("rs485_timeout", "3");
} else {
    timeout = 0;
    timeout_usec = 100000;
    }

// timeout wait for reply 1000ms
tv.tv_sec = timeout;
tv.tv_usec = timeout_usec; //1000000;

retval = select((int)fd_port+1, &rfds, NULL, NULL, &tv);

Solution

  • Because of the randomness of the time and place of the crash, I suspected multithreading problems. I removed all multithreading code and the problems seem to have ceased.