LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-12-2007, 04:54 AM   #1
vdx
Member
 
Registered: Aug 2007
Location: The Greate INDIA
Distribution: CentOS, RHEL, Fedora
Posts: 102

Rep: Reputation: 24
Memory leak in pthread


helo frnds,

I am using RHEL5 and C lang for development.

I am getting some memory leak problem in pthread.

I hav developed a program which creates two threads for listening purpose on two diff ports. both the child threads are doing same job but on diff port no.

I am using pthread_create(), pthread_join() and pthread_kill() thread routines in my program. and wen I checked my program using valgrind tool it gives me some memory leak.

The memory leak occures in libpthread.

so is there any routine to cleanup the thread memory [eg. like pthread_destory()]

following are some lines of out put of valgrind
Quote:
==============================================================
272 bytes in 2 blocks are possibly lost in loss record 5 of 7
at 0x40046FF: calloc (vg_replace_malloc.c:279)
by 0x446479E9: _dl_allocate_tls (in /lib/ld-2.5.so)
by 0x447C79D6: pthread_create@@GLIBC_2.1 (in /lib/libpthread-2.5.so)
by 0x80488BD: main (sigThread.c:50)

LEAK SUMMARY:
definitely lost: 0 bytes in 0 blocks.
possibly lost: 272 bytes in 2 blocks.
still reachable: 1,928 bytes in 6 blocks.
suppressed: 0 bytes in 0 blocks.
Reachable blocks (those to which a pointer was found) are not shown.
To see them, rerun with: --show-reachable=yes
==============================================================
 
Old 10-12-2007, 10:23 AM   #2
orgcandman
Member
 
Registered: May 2002
Location: new hampshire
Distribution: Fedora, RHEL
Posts: 600

Rep: Reputation: 109Reputation: 109
Actually, I've observed the same leak with my own custom heap. I can give you a dump of the bytes leaked if you want. They're leaked during thread creation.

This is where the leak occurs
0x4000dcc4 <allocate_dtv+52>: call 0x4000076c <completed.4497+939223960>
0x4000dcc9 <allocate_dtv+57>: test %eax,%eax

This is the backtrace:
#0 0x4000dc9b in allocate_dtv () from /lib/ld-linux.so.2
#1 0x4000df3c in _dl_allocate_tls () from /lib/ld-linux.so.2
#2 0x441a78e5 in pthread_create@@GLIBC_2.1 () from /lib/tls/libpthread.so.0
#3 0x0804885a in main () at test.c:59

I haven't filed a bug yet, but it's on my list of todos

-Aaron
 
Old 10-12-2007, 10:30 AM   #3
skoona
Member
 
Registered: Mar 2004
Location: Indiana, USA
Distribution: Fedora, CentOS, Ubuntu, OS/X, Raspbian
Posts: 90

Rep: Reputation: 18
vdx,

A unknown memory leak in the pthread libraries for your program is not very likely; although it could be possible based on versions, etc. Lets assume its not a library problem for a moement; it must be something else in your code or your useage of the pthread api's causing the leak. Post some code so we can see what's hapening, or a url.

Last edited by skoona; 10-12-2007 at 10:31 AM.
 
Old 10-12-2007, 01:14 PM   #4
orgcandman
Member
 
Registered: May 2002
Location: new hampshire
Distribution: Fedora, RHEL
Posts: 600

Rep: Reputation: 109Reputation: 109
Quote:
Originally Posted by skoona View Post
vdx,

A unknown memory leak in the pthread libraries for your program is not very likely; although it could be possible based on versions, etc. Lets assume its not a library problem for a moement; it must be something else in your code or your useage of the pthread api's causing the leak. Post some code so we can see what's hapening, or a url.
Actually, as I've said, I observe the same leak, and the code simply calls pthread_create(), prints a string, and exits. My custom heap tags each piece of memory with a backtrace address location, and when the program exists, dumps all the leaked blocks, including their memory contents and the point in code where malloc/calloc/realloc was called.

-Aaron
 
Old 10-12-2007, 02:06 PM   #5
skoona
Member
 
Registered: Mar 2004
Location: Indiana, USA
Distribution: Fedora, CentOS, Ubuntu, OS/X, Raspbian
Posts: 90

Rep: Reputation: 18
orgcandman,

Well said then, you've done all the hard work - except the bug report. I must have some pretty good habits that help me avoid or minimize this exposure. Thanks for confirmation.

Note: from a design point, when I create a thread I usually keep it until program close/shutdown, even reusing it sometimes...
 
Old 10-12-2007, 08:14 PM   #6
orgcandman
Member
 
Registered: May 2002
Location: new hampshire
Distribution: Fedora, RHEL
Posts: 600

Rep: Reputation: 109Reputation: 109
posted:
http://sources.redhat.com/bugzilla/show_bug.cgi?id=5171
 
Old 10-13-2007, 01:55 AM   #7
vdx
Member
 
Registered: Aug 2007
Location: The Greate INDIA
Distribution: CentOS, RHEL, Fedora
Posts: 102

Original Poster
Rep: Reputation: 24
Red face

dear skoona,

as per the discussion, I am writing the buggy code which gives me memory leaks....

here is the code...

Code:
#define TH_LSNR 10
#define TH_DPLR 20
#define PORTNO 5323

short th_lsnr;
short th_dplr;

void *
deployer (void *ptr)
{
  fprintf (stderr, "I am deployer and I m sleeping.....\n");
  sleep (5);
  strcpy ((char *) ptr, "I_AM_DEPLOYER");
  th_dplr = TH_DPLR;
  //pthread_exit(NULL);
  return NULL;
}

void *
listener (void *ptr)
{
  int fd, nfd, clilen, n;
  char buffer[SIZE];

  struct sockaddr_in serv_addr, cli_addr;

  fd = socket (AF_INET, SOCK_STREAM, 0);

  if (fd < 0)
    {
      perror ("SOCKET");
      return NULL;
    }

  bzero ((char *) &serv_addr, sizeof (serv_addr));

  serv_addr.sin_family = AF_INET;
  serv_addr.sin_addr.s_addr = INADDR_ANY;
  serv_addr.sin_port = htons (atoi (PORTNO));

  if (bind (fd, (struct sockaddr *) &serv_addr, sizeof (serv_addr)) < 0)
    {
      perror ("BIND");
      return NULL;
    }

  listen (fd, 5);

  clilen = sizeof (cli_addr);

  fprintf (stderr,"Listening...\n");
  nfd = accept (fd, (struct sockaddr *) &cli_addr, &clilen);
  if (nfd < 0)
    {
      perror ("ACCEPT");
      close (fd);
      return NULL;
    }

  bzero (buffer, SIZE);

  n = read (nfd, buffer, SIZE - 1);
  if (n < 0)
    {
      perror ("READ");
      close (fd);
      close (nfd);
      return NULL;
    }

  close (nfd);
  close (fd);

  //fprintf(stderr,"Server read : %s\n",buffer);

  strcpy ((char *) ptr, buffer);

  th_lsnr = TH_LSNR;

  //pthread_exit(NULL);
  return NULL;
}

int
main ()
{ 
  pthread_t th_listener, th_deployer;
  char retMsg[256];
  int retValLsnr = 0, retValDplr = 0;
  
  th_lsnr = 0;       //global var
  th_dplr = 0;       //global var

  /*
   Create threads "listener" and "deployer"
  */ 
  retValLsnr = pthread_create (&th_listener, 
                        NULL, listener, (void *) retMsg);
  retValDplr = pthread_create (&th_deployer, 
                        NULL, deployer, (void *) retMsg);
  int i = 0;

  /*
  -- Sleep and check wether any of thread exited ?
     Checking is done with the help of global variables 
     (th_lsnr, th_dplr)

  -- If any of thread exited then recreate it

  -- print the message

  -- update the global variables
  */
  while (i++ < 10)
    {
      if (th_lsnr == TH_LSNR)
        {
          pthread_join (th_listener, NULL);
          printf
            ("Listener thread returns: %d and Message = %s\n",
             retValLsnr, (char *) retMsg);
          retValLsnr =
            pthread_create (&th_listener, NULL, listener, (void *) retMsg);
          th_lsnr = retValLsnr = 0;
          bzero (retMsg, sizeof (retMsg));
        }
      if (th_dplr == TH_DPLR)
        {
          pthread_join (th_deployer, NULL);
          printf
            ("Sender thread returns: %d and Message = %s\n",
             retValDplr, (char *) retMsg);
          retValDplr =
            pthread_create (&th_deployer, 
                        NULL, deployer, (void *) retMsg);
          th_dplr = retValDplr = 0;
          bzero (retMsg, sizeof (retMsg));
        }

      sleep (2);
    }
  return 0;
}

The above code gives me the following memory leak info. (by using valgrind utility)

Quote:
==25595== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 13 from 1)
==25595== malloc/free: in use at exit: 1,296 bytes in 3 blocks.
==25595== malloc/free: 3 allocs, 0 frees, 1,296 bytes allocated.
==25595== For counts of detected errors, rerun with: -v
==25595== searching for pointers to 3 not-freed blocks.
==25595== checked 21,029,264 bytes.
==25595==
==25595== LEAK SUMMARY:
==25595== definitely lost: 0 bytes in 0 blocks.
==25595== possibly lost: 272 bytes in 2 blocks.
==25595== still reachable: 1,024 bytes in 1 blocks.
==25595== suppressed: 0 bytes in 0 blocks.
==25595== Reachable blocks (those to which a pointer was found) are not shown.
==25595== To see them, rerun with: --show-reachable=yes
Note : I have removed use of pthread_kill() function, bcoz it couses abnormal termination.

Last edited by vdx; 10-13-2007 at 02:02 AM.
 
Old 10-18-2007, 01:29 AM   #8
vdx
Member
 
Registered: Aug 2007
Location: The Greate INDIA
Distribution: CentOS, RHEL, Fedora
Posts: 102

Original Poster
Rep: Reputation: 24
Unhappy

dear orgcandman, skoona...

I have found a very strange thing.

I am using RHEL5 (glibc-2.5-12).

When I am running the thread program (I have previously posted the whole program) in my RHEL system it couses the memory leak,

BUT

Wen I m running the same programme in FC6(glibc-2.5-3), gives me "no leaks are possible"

I cudnt understnd this....

(I have tested memory leak using valgrind in both the systems).
 
Old 10-18-2007, 01:56 AM   #9
Alien_Hominid
Senior Member
 
Registered: Oct 2005
Location: Lithuania
Distribution: Hybrid
Posts: 2,247

Rep: Reputation: 53
Maybe different Valgrind versions?
 
Old 10-18-2007, 02:41 PM   #10
skoona
Member
 
Registered: Mar 2004
Location: Indiana, USA
Distribution: Fedora, CentOS, Ubuntu, OS/X, Raspbian
Posts: 90

Rep: Reputation: 18
vdx,

Please run valgrind again to see where these blocks are in the code. ""To see them, rerun with: --show-reachable=yes ""

Also, using pthread_exit(NULL) instead of return(NULL) in the threads -- then use pthread_join(th_id, &rtn_value) to get the value. i.e. let it do its designed function of cleaning up after threads. pthread_exit() and pthread_join() work together to do that cleanup. This should produce a different result in valgrind.

Oh, the differences between platforms could be releated to the library release levels. Whare are the pthread library levels for your two platforms?
 
Old 10-18-2007, 08:47 PM   #11
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
The only user memory unique to a certain thread is its stack. If it allocates dynamic memory there is no reason for that memory to be freed when that thread ceases to exist. It isn't like a separate process.

Threads are simple in that they are just a function which is being executed parallel to the thread of control (or "main thread".) The only exit handlers they have are the stack unwind, which should be called when that thread exits. Additionally, I believe pthread_kill is not for killing threads: it's only to have a signal processed on the stack of that thread, interrupting its execution. pthread_cancel is the appropriate way to stop a thread, which will unwind the stack in the state it's in when the call is executed. This can cause a lot of problems unless your code is extremely thread safe and literally may unwind at any point (even between a free call and assignment to NULL.)

I personally think there is no safe way to kill a thread. I always use some sort of flag which the thread checks between operations so that it can exit logically. In other words, I'd rethink how you end the thread.
ta0kira
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
memory leak ? os2 Programming 1 05-19-2005 01:45 PM
Memory Leak?! RoaCh Of DisCor Linux - General 9 05-07-2005 12:26 AM
Possible Memory Leak liquidcool Linux - General 3 10-07-2004 08:38 AM
possible memory leak?? matt80 Linux - General 2 07-01-2004 11:07 PM
Memory Leak when using memory debugging C program on SuSE SLES8 babalina Linux - Distributions 0 10-06-2003 09:39 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration