You pass a pointer to a local variable (parameter connection_descriptor
) to a new thread. By the time the thread is created and executed, the pop_protocol
function most probably returns, and the local variable is not valid any more (in fact, its value can still exist at the address that was passed to the new thread, but it's undefined behaviour.)
Try allocating memory for the connection_descriptor
with malloc
, passing that memory (not a local variable!) to the thread being created, and free
that memory inside the thread's function after the socket id is copied into a local variable.