[PD-dev] pd external memory weirdness

tim redfern pd at eclectronics.org
Wed Apr 16 18:42:30 CEST 2008


Hi.. yup absolutely , theres something wrong with the way my code is
interacting with pd and threads .. however what I'm doing is so simple I
have to conclude there is something very strange amiss..

On Tue, 2008-04-15 at 14:56 -0400, Hans-Christoph Steiner wrote:
> I'd say it's most likely your code.  I would be very surprised if  
> libc is leaking memory.  There are thousands of eyes looking at that  
> code, and many millions using it in many ways.  Pd code could have  
> leaks, but mostly it is good.
> 
> Try posting your code.
> 

I've stripped this down to the simplest example possible.
When this external is used, it has a bang method that launches a thread
that does absolutely nothing. It also mallocs 1000000 bytes and frees
them immediately (not even within the thread).

The test patch is::

#N canvas 0 0 450 300 10;
#X obj 31 88 thread;
#X obj 42 47 metro 1000;
#X obj 39 8 tgl 15 0 empty empty empty 0 -6 0 10 -262144 -1 -1 0 1
;
#X obj 126 109 bng 15 250 50 0 empty empty empty 0 -6 0 10 -262144
-1 -1;
#X connect 1 0 0 0;
#X connect 1 0 3 0;
#X connect 2 0 1 0;

As far as I can see, this leaks all of the memory that its allocating.
The VIRT column of the pd entry in 'top' climbs at more than 1 meg per
second.

However, if I comment out pthread_create then pd's memory use doesnt
grow at all.

Heres the code...what am I doing wrong?

#include "m_pd.h"
#include <stdlib.h>

#include <pthread.h>

pthread_attr_t gen_attr;

static t_class *thread_class;

typedef struct _thread {
  t_object  x_obj;
} t_thread;

void *threadtester( void* x_obj  ) {
        t_thread *x= (t_thread *) x_obj;

        //do nothing in particular

        return 0;
}

void thread_bang(t_thread *x)
{
        char* temp=(char*)malloc(1000000);
        free(temp);
        
        pthread_t stest;
        pthread_create (&stest,&gen_attr,threadtester,(void *) x);
        post("hello world!");
}

void *thread_new(void)
{
  t_thread *x = (t_thread *)pd_new(thread_class);

  return (void *)x;
}

void thread_setup(void) {
        //
        //create parameters for low priority thread
        pthread_attr_init(&gen_attr);
        struct sched_param param;
        param.sched_priority = 15;  //LOW priority (-20..20)
        pthread_attr_setschedparam( &gen_attr, &param );
        
        thread_class = class_new(gensym("thread"),
                (t_newmethod)thread_new,
                0, sizeof(t_thread),
                CLASS_DEFAULT, 0);
        class_addbang(thread_class, thread_bang);
}







More information about the Pd-dev mailing list