UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2008-03-19 03:42 AM

frank2004
Member
From: Beijing
Registered: 2004-07-20
Posts: 93

Re: pthread memory leak(Linux)

On Linux, we create a concurrent tcp server to accept the connection, once the there comes the connect, we create a thread for it to do something, the problem is that this program cause the memory leak, the code is:

int ptop_init(char *service, void *srv(int))
    {
    static pthread_t thread[4096];      /* The thread handler array of available threads. */
    static DB_SRV pSRV;

    int sockfd,newsockfd;       /* The opened socket id of system call 'socket' & 'accept' */

    int length;
    struct sockaddr_in tcp_cli_addr;

    pSRV=(DB_SRV)srv;   // thread routinue.
    if((sockfd=tcp_init(service,0))<0){
      fprintf(stderr,"ptop_init error when tcp_init\n");
      exit(-1);
      }

    while(1){
      newsockfd=tcp_accept(sockfd,(struct sockaddr *)&tcp_cli_addr,&length);
         if(newsockfd<0){
        fprintf(stderr,"ptop_init error when tcp_accept\n");
        exit(-1);
        }
      pthread_detach(&thread[newsockfd])
      if(pthread_create(&thread[newsockfd],pthread_attr_default,(pthread_startroutine_t)pSRV,\
                (pthread_addr_t)newsockfd)==-1){
        perror("pthread_create error in ptop_init");
        exit(-1);
        }
      }

    pthread_exit(0);
    return 0;
}

coresponding to the above pSRV handler, it calls select and processing the packets,and call close(sockfd) and pthread_exit(0).
anyone can point some suggestion about it?  can it fix if put the pthread_detach after the pthread_create?  ...?

by the way, the above route has no problem on HP-UX,AIX,True64 UNIX.

Offline

#2 2008-03-19 07:24 AM

mlampkin
Administrator
From: Sol 3
Registered: 2002-06-12
Posts: 911
Website

Re: pthread memory leak(Linux)

One item that jumps out is :

pthread_detach(&thread[newsockfd])
      if(pthread_create(&thread[newsockfd],pthread_attr_default,(pthread_startroutine_t)pSRV,\
                (pthread_addr_t)newsockfd)==-1){
        perror("pthread_create error in ptop_init");

You are detaching the thread before it is even created... if you check the return value of the call to pthread_detach( ... ) in your code I am certain you will see it returning an error code... so the threads are never being detached and ( aren't being joined anywhere ) they will be eating / leaking memory...

Just move the pthread_detach call to after the pthread_create and I am guessing all will be well...

Btw - an alternative to detaching a thread is via the attributes e.g. :

pthread_t thread;
   pthread_attr_t attr;

   ...

   pthread_attr_init( & attr );
   pthread_attr_setdetachstate( & attr, PTHREAD_CREATE_DETACHED );

   pthread_create( & thread, & attr, & start_function, NULL );

Hope that helps...


Michael


"The only difference between me and a madman is that I'm not mad."

Salvador Dali (1904-1989)

Offline

#3 2008-03-19 07:47 AM

frank2004
Member
From: Beijing
Registered: 2004-07-20
Posts: 93

Re: pthread memory leak(Linux)

I also guess something wrong with pthread_detach,we'll try it , thanks you.

Offline

#4 2008-06-15 06:14 AM

developer_chris
Member
Registered: 2008-06-05
Posts: 21

Re: pthread memory leak(Linux)

mlampkin;24110 wrote:

Btw - an alternative to detaching a thread is via the attributes e.g. :

pthread_t thread;
   pthread_attr_t attr;

   ...

   pthread_attr_init( & attr );
   pthread_attr_setdetachstate( & attr, PTHREAD_CREATE_DETACHED );

   pthread_create( & thread, & attr, & start_function, NULL );

Hope that helps...


Michael

You still need to call pthread_detach even if you create it detached.

it doesnt make logical sense to create it detached and then have to detach it anyway, but you do. This is what the docs say...

The pthread_join() or pthread_detach() functions should eventually be called for every thread that is created so that storage associated with the thread may be reclaimed.

It has been suggested that a "detach" function is not necessary; the detachstate thread creation attribute is sufficient, since a thread need never be dynamically detached. However, need arises in at least two cases:
1.  In a cancellation handler for a pthread_join() it is nearly essential to have a pthread_detach() function in order to detach the thread on which pthread_join() was waiting. Without it, it would be necessary to have the handler do another pthread_join() to attempt to detach the thread, which would both delay the cancellation processing for an unbounded period and introduce a new call to pthread_join(), which might itself need a cancellation handler. A dynamic detach is nearly essential in this case.


2.  In order to detach the "initial thread" (as may be desirable in processes that set up server threads).

I don't pretend to understand exactly why but until I realised that, I had a persistent memory leak I could not find.
Even if you detach it in some libraries the leak remains :(

Offline

#5 2015-11-16 03:03 PM

Srikar
Guest

Re: pthread memory leak(Linux)

To my understanding the documentation is arguing about detach function not being necessary to be implemented or supported. It is not arguing about "call to detach function" being necessary even if attribute was initially set to PTHREAD_CREATE_DETACHED.

Board footer

Powered by FluxBB