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)

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 );

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