Good way to save a t_dictionary in the patcher ?

Jun 6, 2012 at 3:35pm

Good way to save a t_dictionary in the patcher ?

Hello,

i need to store a t_dictionary in the patcher ; for that i use appendtodictionary method.
My question is : do i need to chuck the entry to keep and use the dictionary reloaded ? Is the code below correct ?

if (x = (t_flop *)object_alloc (flop_class)) {
    t_dictionary *d = NULL; 

    if (d = (t_dictionary *)gensym ("#D")->s_thing) {
        if (dictionary_hasentry (d, gensym ("toto")) && dictionary_entryisdictionary (d, gensym ("toto"))) {
            dictionary_getdictionary (d, gensym ("toto"), (t_object **)&x->data);
            dictionary_chuckentry    (d, gensym ("toto"));

        } else {
            x->data = dictionary_new ( );
        }
    }
}

Thanks.

Attachments:
  1. dictionary.zip
#56481
Jun 9, 2012 at 2:41pm

Hello,

… or is the good way is to make a copy of the dictionary in the toto_new method ?

Do i have to free it in my toto_freemethod ?
If yes, in this case i must provide a copy to the “toto_ appendtodictionary” method, isn’t it ?

Thanks for any clue.

#202496
Jun 9, 2012 at 3:38pm

Hello third,

so finally i do that :

1. chuck entry at load.
2. clone the dictionary in appendtodictionary method.
3. free the dictionanry in free method.

You think it’s not correct ? Please tell me more ;-)

Attachments:
  1. Dictionary2.zip
#202497
Jun 19, 2012 at 3:52pm

In the initial post, I think your approach should work.

It is important in your example to chuck the entry after you’ve gotten a pointer to the ‘toto’ dictionary to ensure that when the dictionary arg is freed it does not also free your ‘toto’ dictionary. Unless that dictionary is huge, however, you could also clone the ‘toto’ dictionary.

Either way, once you have a pointer to the ‘toto’ dictionary, and there is no parent dictionary referencing ‘toto’, then yes you are responsible for freeing it when it is no longer needed.

best,
Tim

#202498

You must be logged in to reply to this topic.