[PD-dev] allow only one instance of external object

Jamie Bullock jamie at postlude.co.uk
Mon Mar 24 11:59:01 CET 2008


Hi,

Try this:

----------------------------------------

#include "m_pd.h"

static t_class *counter_class;
static int instance;

typedef struct _counter {
  t_object  x_obj;
} t_counter;

static void *counter_new(t_symbol *s, t_int argc, t_atom *argv)
{
  t_counter *x = (t_counter *)pd_new(counter_class);
  instance++;
  post("Instance %d", instance);
  return (void *)x;
}

static void counter_free(t_counter *x){
    instance--;
}

void counter_setup(void) {
  counter_class = class_new(gensym("counter"),
        (t_newmethod)counter_new,
        (t_method)counter_free, sizeof(t_counter),
        CLASS_DEFAULT, 0);

  instance = 0;
} 

-------------------------

That implements Claude's suggestion, and definitely works. If your
external still crashes, then perhaps think about debugging it:

http://puredata.info/docs/developer/DebuggingPdExternals

Jamie 

--
www.postlude.co.uk

On Mon, 2008-03-24 at 00:07 +0400, best boy wrote:
> hi
> yes, it's for an external i'm writing.
> i tried the global counter variable thing you recommended and it
> works, except for
> when the first instance is duplicated, or copy/pasted in a patcher.
> this crashes pd.
> 
> do you, or anyone else, know of a working example of the global
> counter variable thing?
> 
> thanks
> olli
> 
> > Date: Sun, 23 Mar 2008 10:43:36 +0000
> > From: claudiusmaximus at goto10.org
> > To: ladataxi at hotmail.de
> > CC: pd-dev at iem.at
> > Subject: Re: [PD-dev] allow only one instance of external object
> > 
> > best boy wrote:
> > > hi
> > > is there a way to keep track of the number of opened instances of
> an 
> > > external?
> > 
> > Is this an external you are writing? Then yes. Otherwise there's
> weird 
> > tricks you can probably do to keep track, but not control.
> > 
> > > if so, can anyone point me to an example object?
> > 
> > Just have a global counter variable that you initialise to 0 in 
> > foo_setup(), and in foo_new() you increment it, and in foo_free()
> you 
> > decrement it.
> > 
> > > thanks
> > > olli
> > 
> > Claude
> > -- 
> > http://claudiusmaximus.goto10.org
> > 
> 
> 
> ______________________________________________________________________
> Jetzt dabei sein und Windows Live Mail testen. Windows Live Mail.
> _______________________________________________
> PD-dev mailing list
> PD-dev at iem.at
> http://lists.puredata.info/listinfo/pd-dev





More information about the Pd-dev mailing list