Static binbuf free at quit?


    Jun 26 2007 | 1:41 am
    I'm using a static binbuf, into which I have patchers save themselves. I use a static t_patcher to check if I'm in the same parent patcher as the last time any instance of my object saved a binbuf, and just use the cached static binbuf if so.
    static t_object *obj_bb = NULL; // the binbuf static t_patcher *obj_pp = NULL; // the patcher
    int main() { if(obj_bb == NULL) obj_bb = binbuf_new(); // init the binbuf }
    ...later...
    if(obj_pp != pp) // pp is object instance parent patcher { binbuf_set(obj_bb, 0, 0, NULL); // nuke the old binbuf mess1((Object *)pp, gensym("save"), obj_bb); // save into it obj_pp = pp; // remember the new current parent patcher }
    This cuts down my init time considerably where a parent patcher contains lots of patchers containing my object - I save the parent patcher to binbuf once.
    Should I free the static binbuf at quit? If so, how? Is this a suitable job for a quit task - which would presumably need to check the binbuf is not already freed by an earlier object quit task?

    • Jun 26 2007 | 10:54 am
      quittasks are only triggered once per class.
      Another strategy: every time _new() is called, increment an int variable by 1. every time _free() is called, decrement the same variable by 1. Call the var foo.
      So, in new()
      ... if (!foo) obj_bb = binbuf_new(); foo++;
      And in free()
      ... foo--; if (!foo) binbuf_free(obj_bb);
      Then you can spare yourself the quittask, if you're uncomfortable using it.
      jb
      Am 26.06.2007 um 03:41 schrieb John Pitcairn:
      > > I'm using a static binbuf, into which I have patchers save > themselves. I use a static t_patcher to check if I'm in the same > parent patcher as the last time any instance of my object saved a > binbuf, and just use the cached static binbuf if so. > > static t_object *obj_bb = NULL; // the binbuf > static t_patcher *obj_pp = NULL; // the patcher > > int main() > { > if(obj_bb == NULL) obj_bb = binbuf_new(); // init the binbuf > } > > ...later... > > if(obj_pp != pp) // pp is object instance parent patcher > { > binbuf_set(obj_bb, 0, 0, NULL); // nuke the old binbuf > mess1((Object *)pp, gensym("save"), obj_bb); // save into it > obj_pp = pp; // remember the new current parent patcher > } > > This cuts down my init time considerably where a parent patcher > contains lots of patchers containing my object - I save the parent > patcher to binbuf once. > > Should I free the static binbuf at quit? If so, how? Is this a > suitable job for a quit task - which would presumably need to check > the binbuf is not already freed by an earlier object quit task?
    • Jun 26 2007 | 11:07 pm
      Quote: Jeremy Bernstein wrote on Tue, 26 June 2007 22:54 ---------------------------------------------------- > quittasks are only triggered once per class.
      Thanks, and I have no objection to using a quittask. When it runs, do I still have access to the class's static binbuf from within the quittask method (installed with no args), or do I need to pass in a reference to the binbuf when installing the quittask?