Freeing/clearing a dictionary without causing memory leaks

Luigi Castelli's icon

Hi folks,

I have a scenario that makes me unsure about whether I am leaking memory or not.
Here's the (simplified) code snippet in question:

t_dictionary *d;
t_dictionary *sd;
t_atom argv[64];
long argc = 64;
long i;

d = dictionary_new();
if (d) {
    for (i = 0; i < argc; i++) {
        sd = dictionary_new();
        if (sd) {
            dictionary_appendlong(sd, gensym("data"), 74);
        }
        atom_setobj(argv + i, sd);
    }
    dictionary_appendatoms(d, gensym("subdictionary_list"), argc, argv);

    // do more stuff with the dictionary
    // and finally call...

    dictionary_clear(d);
}

Now, when calling dictionary_clear() will all my subdictionaries be automatically freed ?

The reason I am asking is that I know that when appending atoms to a dictionary the atoms will be converted to an t_atomarray. So I basically have an t_atomarray of object pointers appended to my main dictionary.
When clearing (or freeing?) an t_atomarray the API docs say that "(it) does not perform a 'deep' free, meaning that any #A_OBJ atoms will not have their object's freed. Only the references to those objects contained in the atomarray will be freed."

Hence I am unsure whether I have to manually free all subdictionaries or whether dictionary_clear() will automatically take care of that for me.

Thanks for any clarification.

- Luigi

Jeremy's icon

If the atomarray contains objects, they will be auto-freed with the dictionary (dictionary_appendatoms enables the "deep free" behavior of the atomarray in this case).

jb

Luigi Castelli's icon

Great, that's exactly what I needed to know. Thanks Jeremy!

- Luigi