Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #5851

Re: pthread memory leaks

From Ian Collins <ian-news@hotmail.com>
Newsgroups comp.lang.c++
Subject Re: pthread memory leaks
Date 2011-05-31 11:50 +1200
Message-ID <94ioqnFr2iU10@mid.individual.net> (permalink)
References <9430fe32-d385-49a3-b925-daf3cd421789@j13g2000pro.googlegroups.com>

Show all headers | View raw


On 05/31/11 11:29 AM, jakash3 wrote:
> I'm experimenting with signals and conditional variables  to implement
> thread suspension. The following appears to execute without any
> problems but valgrind reports memory leaks.
> What can I do to stop the memory leaks? Is there some type of pthread
> related memory free function I'm forgetting?

You'd be better of on a Linux or Unix group for this kind of question. 
However there are a few C++ specific issues:

> =================================
> #include<pthread.h>
> #include<signal.h>
> #include<unistd.h>
> #include<cstdio>
> #include<cstring>
> #include<cstdlib>
>
> pthread_mutex_t m;
> pthread_cond_t c;
>
> void freeze(int sig) {
> 		pthread_mutex_lock(&m);
> 		pthread_cond_wait(&c,&m);
> 		pthread_mutex_unlock(&m);
> }
> void quit(int sig) { pthread_exit(0); }
>
> void* tfunc(void* param) {
> 	while (1) {
> 		puts("hi");

As you have included the C++ versions of the C library headers, fictions 
form them like puts should be written std::puts.  Some compilers put 
them in the namespace, while others (like gcc) declare them in and out 
of the namespace, which is a pain.

> 		sleep(1);
> 	}
> 	return NULL;
> }

This are all assigned to C function pointers, so they should be declared 
as extern "C" (gcc ignores this).

<snip code>

> valgrind output:
> =================================
> ==2993==
> ==2993== HEAP SUMMARY:
> ==2993==     in use at exit: 144 bytes in 1 blocks
> ==2993==   total heap usage: 1 allocs, 0 frees, 144 bytes allocated
> ==2993==
> ==2993== LEAK SUMMARY:
> ==2993==    definitely lost: 0 bytes in 0 blocks
> ==2993==    indirectly lost: 0 bytes in 0 blocks
> ==2993==      possibly lost: 144 bytes in 1 blocks

Note this is an in use block, not an actual leak.  Probably an internal 
I/O buffer.

-- 
Ian Collins

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

pthread memory leaks jakash3 <jakashthree@gmail.com> - 2011-05-30 16:29 -0700
  Re: pthread memory leaks Ian Collins <ian-news@hotmail.com> - 2011-05-31 11:50 +1200
    Re: pthread memory leaks Ian Collins <ian-news@hotmail.com> - 2011-05-31 11:52 +1200
      Re: pthread memory leaks Puppet_Sock <puppet_sock@hotmail.com> - 2011-05-31 06:46 -0700
    Re: pthread memory leaks jakash3 <jakashthree@gmail.com> - 2011-05-30 17:00 -0700
      Re: pthread memory leaks Ian Collins <ian-news@hotmail.com> - 2011-05-31 12:02 +1200
        Re: pthread memory leaks jakash3 <jakashthree@gmail.com> - 2011-05-30 17:05 -0700
          Re: pthread memory leaks yatremblay@bel1lin202.(none) (Yannick Tremblay) - 2011-06-06 09:38 +0000
  Re: pthread memory leaks Stuart Redmann <DerTopper@web.de> - 2011-05-31 00:16 -0700
    Re: pthread memory leaks "Chris M. Thomasson" <cristom@charter.net> - 2011-06-10 15:46 -0700
  Re: pthread memory leaks Adam Skutt <askutt@gmail.com> - 2011-05-31 03:20 -0700
    Re: pthread memory leaks Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-06-01 08:27 +0000
      Re: pthread memory leaks Miles Bader <miles@gnu.org> - 2011-06-01 17:39 +0900
      Re: pthread memory leaks Ian Collins <ian-news@hotmail.com> - 2011-06-01 21:05 +1200
        Re: pthread memory leaks Adam Skutt <askutt@gmail.com> - 2011-06-01 03:27 -0700
      Re: pthread memory leaks Adam Skutt <askutt@gmail.com> - 2011-06-01 03:16 -0700

csiph-web