Thread: Memory leak on pthread_create?

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    117

    Memory leak on pthread_create?

    Whenever I run valgrind on my program, it is telling that I have possibly lost memory wherever I call pthread_create. I have been trying to follow the guidance on

    valgrind memory leak errors when using pthread_create pthread_create valgrind memory leak solved « Gelorakan …

    and other various websites google gave me, but nothing has worked. So far I have tried joining the threads, setting an pthread_attr_t to DETACHED, calling pthread_detach on each thread, and calling pthread_exit().

    trying PTHREAD_CREATE_DETACHED -

    Code:
    pthread_attr_t attr;
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
    
    pthread_create(&c_udp_comm, &attr, udp_comm_thread, (void*)this);
    pthread_create(&drive, &attr, driving_thread, (void*)this);
    pthread_create(&update, &attr, update_server_thread(void*)this);

    I think I may have coded this next one with joining wrong...I am going by https://computing.llnl.gov/tutorials/pthreads/ and they have all their threads in an array so they just for loop. But I don't have them all in an array so I tried to just change it to work. Please tell me if I did it wrong.

    Code:
    void* status;
    pthread_attr_t attr;
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
    
    pthread_create(&c_udp_comm, &attr, udp_comm_thread, (void*)this);
    pthread_create(&drive, &attr, driving_thread, (void*)this);
    pthread_create(&update, &attr, update_server_thread(void*)this);
    
    pthread_join(c_udp_comm, &status);
    pthread_join(drive, &status);
    pthread_join(update, &status);

    trying pthread_detach -


    Code:
    pthread_create(&c_udp_comm, NULL, udp_comm_thread, (void*)this);
    pthread_create(&drive, NULL, driving_thread, (void*)this);
    pthread_create(&update, NULL, update_server_thread(void*)this);
    
    pthread_detach(c_udp_comm);
    pthread_detach(drive);
    pthread_detach(update);

    trying pthread_exit -

    Code:
    pthread_create(&c_udp_comm, NULL, udp_comm_thread, (void*)this);
    pthread_create(&drive, NULL, driving_thread, (void*)this);
    pthread_create(&update, NULL, update_server_thread(void*)this);
    
    pthread_exit(NULL);
    If anyone can help me figure out why none of this is working I would be very grateful.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Memory leak on pthread_create - CodeGuru Forums

    If you write a simple, complete program and post it, we'll tell you if it contains any memory leaks - then compare that to what Valgrind says and go from there.

    gg

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Read the examples here -> https://computing.llnl.gov/tutorials...reatingThreads
    main() ends by calling pthread_exit().

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <errno.h>
    #include <unistd.h>
    #include <pthread.h>
    
    void* process( void* data ) {
      printf( "hello world\n" );
    }
    
    int main() {
      pthread_t th;
      pthread_attr_t attr;
      pthread_attr_init(&attr);
      pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
      pthread_create( &th, &attr, process, NULL );
      pthread_attr_destroy(&attr);
      //pthread_exit(0);  // no leak when this is in the code.
      // dummy
      return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    117
    Whenever I run that code, with or without the explicit pthread_exit, I still get a possible memory loss of 144 bytes each time. So I should just regard that as wrong?

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by SterlingM View Post
    Whenever I run that code, with or without the explicit pthread_exit, I still get a possible memory loss of 144 bytes each time. So I should just regard that as wrong?
    Maybe. When I run that code with pthread_exit(), I get a leak of 272 bytes in one block allocated by pthread_create(). Without pthread_exit(), there is no leak.

    But pthread_exit() is pretty vital if you have detached threads and main() ends. Try this one:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <pthread.h>
    
    void* process( void* data ) {
      printf("%ld hello world\n", (long int)data);
      return NULL;
    }
    
    int main() {
    	pthread_t th[3];
    	long int i;
    	for (i = 0; i < 3; i++) {
    		pthread_create(&th[i], NULL, process, (void*)i);
    		pthread_detach(th[i]);
    	}
    
    	pthread_exit(0);
    	return 0;
    }
    I get "All heap blocks were freed -- no leaks are possible" for that with or without pthread_exit (but without pthread_exit not all threads complete). Note that that pthread_exit() terminates main, ie, the return statement is not executed.

    Quote Originally Posted by http://cursuri.cs.pub.ro/~apc/2003/resources/pthreads/uguide/users-18.htm#285690
    Termination of the initial thread via pthread_exit() or any thread termination mechanism terminates the entire process.
    Just returning from main only ends that thread and I believe condemns any incomplete threads spawned in main to some kind of weird oblivion.
    Last edited by MK27; 09-05-2011 at 11:05 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    It's always possible that there is a bug in your library implementation of pthread, or even a bug in valgrind.

    If you demonstrate that you have the problem in a really simple test case (as above), and your main code has no other issues, then I think you should be able to claim success.

    BTW, I have
    $ valgrind --version
    valgrind-3.6.0.SVN-Debian
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Possible memory leak.
    By msh in forum C++ Programming
    Replies: 11
    Last Post: 07-21-2009, 12:28 PM
  2. memory leak
    By aruna1 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2008, 10:28 PM
  3. Memory Leak
    By jtullo in forum C Programming
    Replies: 7
    Last Post: 12-11-2006, 11:45 PM
  4. Replies: 2
    Last Post: 09-28-2006, 01:06 PM
  5. Why is this a memory leak?
    By deadpoet in forum C++ Programming
    Replies: 2
    Last Post: 02-20-2004, 11:16 AM