Hi,

I've created a sample program that demonstrates some memory problems, at least
according to ccmalloc. The code is below.

Note I've intentionally created a leak at the top of main (999 bytes) so you
can see what ccmalloc output looks like.

One very strange thing below is, if I undefine COND below to not use any
mutexes or condition variables, no leaks are reported. However, if I use them,
leaks are reported, including a big one (64K), which I presume is the thread
stack that isn't being released properly. 

To build:

g++ test.cpp -l pth -lccmalloc -ldl -o thread

Please advise!!! Thanks.

Sincerely

Brent

----code (test.cpp)----
#include "pth.h"
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

extern FILE *stdout;
bool done = false;

#define COND

pth_mutex_t mutex = PTH_MUTEX_INIT;

pth_cond_t cond = PTH_COND_INIT;

static void *handler(void *_arg)
{

  pth_nap(pth_time(5, 2));
  done = true;
 
#ifdef COND
  pth_cond_notify(&cond, 0);
#endif
  
  pth_exit(0);
}

int main() {

  pth_init();

  void* morestuff = malloc(999);

  pth_attr_t attr = pth_attr_new();

  pth_attr_set(attr, PTH_ATTR_JOINABLE, FALSE);

  pth_spawn(attr, handler, NULL);

  printf("in main thread; about to wait\n");

#ifdef COND
  pth_mutex_acquire(&mutex, FALSE, NULL);
  pth_cond_await(&cond, &mutex, NULL);
  pth_mutex_release(&mutex);
#endif

  while (!done)
    pth_yield(NULL);

  printf("main thread exiting\n");

  pth_attr_destroy(attr);

  pth_kill();

  return 0;
}








At 03:12 PM 4/19/2001 -0700, you wrote:
>Brent Phillips wrote:
>> I've found that either there are some serious memory leaks in pTh, or I'm
>> not using it properly. The most serious problem is that the stack space is
>> not freed when a thread terminates. This quickly causes applications to run
>> out of memory.
>
>Are you remembering to pth_join() your terminated threads?
>
>> To test this, I ran a few pth programs with a memory leak detector,
>> ccmalloc
(<http://freshmeat.net/projects/ccmalloc>http://freshmeat.net/projects/ccma
lloc). It turns up all sorts of
>> memory leaks in pth- everything from the stack space to the attributes to
>> the wait functions create memory leaks. (It is very simple to install and
>> use.)
>> 
>> Please advise! Am I missing certain clean up functions? Or are these
>> serious bugs in pTh?
>
>In my experience I haven't encountered any real leaks.. although leaks
>are easy to have if you aren't careful (sometimes this requires looking
>at the source to see if/when a leak can happen).
>
>For example, if you have a thread that can be cancelled, then any memory
>you malloc() with that thread must be free'd by a shutdown hook.
>
>> Pretty much any pTh program that spawns a thread should exhibit this
>> behavior. I can supply sample code if necessary.
>
>I'd be interested in seeing some code that demonstrates leaks.
>
>-Archie
>
>__________________________________________________________________________
>Archie Cobbs     *     Packet Design     *    
<http://www.packetdesign.com/>http://www.packetdesign.com
>______________________________________________________________________
>GNU Portable Threads (Pth)           
<http://www.gnu.org/software/pth/>http://www.gnu.org/software/pth/
>User Support Mailing List                            [EMAIL PROTECTED]
>Automated List Manager (Majordomo)           [EMAIL PROTECTED]
> 

______________________________________________________________________
GNU Portable Threads (Pth)            http://www.gnu.org/software/pth/
User Support Mailing List                            [EMAIL PROTECTED]
Automated List Manager (Majordomo)           [EMAIL PROTECTED]

Reply via email to