when to call sysmem_freeptr?

Mar 8, 2013 at 5:34pm

when to call sysmem_freeptr?

Hi,

I’d like to know when to definitely call sysmem_freeptr
and why Max crashes in some cases even when pointers are local.

Maybe it would be great if anyone who reads the post and can think of an unnamed case
when to call sysmem_freeptr (or handle) or not to just adds it to the post.

I was getting unsure because of the following method:

t_object* get_object_by_scr_name(t_generic *x, t_symbol* scr_name_to_find)
{
    t_object *parent_patcher = NULL;
    t_object* obj_to_find = NULL;
    t_symbol *scr_name_of_obj = NULL;
    t_object *box = NULL;

    //get the object's parent patcher
    t_max_err err = object_obex_lookup(x, gensym("#P"), (t_object **)&parent_patcher);
    if (err != MAX_ERR_NONE)
    {
        //if parent patcher not found
        return NULL;
    }

    box = jpatcher_get_firstobject(parent_patcher); 

    while(box)
    {
        scr_name_of_obj = jbox_get_varname(box); // scripting name
        if (scr_name_of_obj == scr_name_to_find)
        {
            obj_to_find = jbox_get_object(box);
            break;
        }
        box = jbox_get_nextobject(box); // iterate
    }
    if (scr_name_of_obj != scr_name_to_find)
    {
        object_error((t_object*)x, "object not found");
    }

// crashes if all sysmem_freeptr are executed
//    sysmem_freeptr(parent_patcher);
//    sysmem_freeptr(scr_name_of_obj);
//    sysmem_freeptr(box);

    return obj_to_find;
}

?

Greetings,
Alex

#66958
Mar 8, 2013 at 6:42pm

Hi,

1. In general you should free memory only if you have allocated it (on the heap) ; so use of “sysmem_freeptr” is most of times paired with “sysmem_newptr”.

2. In your example those functions (get / lookup) return pointers to owner structures, not copies, so don’t free them !

3. Never free a t_symbol. Always use “gensym” to allocate one.

4. Sometimes functions return memory that you are responsible to free. Which and How is annotated in the SDK.

5. Exceptionaly there is undocumented behavior like “atom_gettext” ; you can search in examples provided with the SDK, through Jamoma or other repositories like my “Flop” that i did specially for this kind of situation ( https://github.com/nicolasdanet/Flop ).

6. For specific questions ask in the forum. I collect responses and append examples in my repository, for noobs and for experts blackouts ;-)

HTH

#241003
Mar 8, 2013 at 7:16pm

Hi Nicolas,
yes this helps a lot. Thank you once again!

#241004

You must be logged in to reply to this topic.