Getting Leaks Even After pthread_detach <p>I am trying to make self-cleanup thread code to release pthread_t resources if I terminate the whole program from a side thread using pthread_detach, but I am still getting memory leaks reports from valgrind with possibly lost bytes. Here is my sample code snippet:</p> <pre><code>pthread_t main_thread; pthread_t second_thread; void* thread_func() { pthread_detach(pthread_self()); exit(0); } int main() { main_thread = pthread_self(); // record main thread in case needed later pthread_create(&amp;second_thread, NULL, thread_func, NULL); while(1); // making main thread wait using a busy-wait (in case pthread_join) interferes // with pthread_detach (that's another question though: does pthread_join called // from another thread overlaps with pthread_detach from the same thread?) } </code></pre> <p><br></p> <p>Can anyone please help me indicate where I forgot to release any allocated resources?</p>