Skip to content
Tcl Source Code View Ticket
Login

View Ticket

Closed / Fixed1e62dd74f70cf7ba7af3b34f0dfeffc9db529665Created 6 years agoUpdated 6 years ago
Descriptionhtml2021-04-01 20:12:15

I'm a beginner at Tcl C API and I'm trying to understand how to use it. I have this code that gets a tcl list from get_my_list proc and then I iterate over it to dispatch some info, in this case, info related to attributes A, B and C that I get with get_attr_info proc. When I run it in a very simple example that basically has only this code, everything works perfectly, but when I add this lib in a big tcl project, eventually it crashes. I suspect that it's due to bad usage of reference counting of tcl objects, I mean, their lifetimes. What could I be doing wrong in the example below? I'm using Tcl 8.6.

    Tcl_Obj* Get_Info(Tcl_Interp *interp, const char* info, const char* attr) {
        char cmd[256];
        sprintf(cmd, "get_attr_info %s %s", info, attr);
        Tcl_Eval(interp, cmd);
        return Tcl_GetObjResult(interp);
    }
    
    static int Copy_Info(ClientData cdata, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) {
        Tcl_Eval(interp, "get_my_list");
        Tcl_Obj *const my_list = Tcl_GetObjResult(interp);
        int my_list_size;
        Tcl_ListObjLength(interp, my_list, &my_list_size);
        Tcl_IncrRefCount(my_list);
    
        for (int i = 0; i < my_list_size; ++i) {
            Tcl_Obj* current_info_obj;
            Tcl_ListObjIndex(interp, my_list, i, &current_info_obj);
            const char* current_info_ctr = Tcl_GetStringFromObj(current_info_obj, NULL);
    
            /* getting info A */
            Tcl_Obj* info_a_obj = Get_Info(current_info_ctr, "A");
            const char* info_a = Tcl_GetStringFromObj(info_a_obj, NULL);
            Copy_Info_A(info_a);
    
            /* getting info B */
            Tcl_Obj* info_b_obj = Get_Info(current_info_ctr, "B");
            const char* info_b = Tcl_GetStringFromObj(info_b_obj, NULL);
            Copy_Info_B(info_b);
    
            /* getting info C */
            Tcl_Obj* info_c_obj = Get_Info(current_info_ctr, "C");
            const char* info_c = Tcl_GetStringFromObj(info_c_obj, NULL);
            Copy_Info_C(info_c);
        }
    
        Tcl_DecrRefCount(my_list);
        Tcl_FreeResult(interp);
    
        return TCL_OK;
    }

jan.nijtmanswiki2021-04-02 06:39:59

From the man-page of Tcl_FreeResult():

    Tcl_FreeResult performs part of the work of Tcl_ResetResult. It frees up the
    memory associated with interp's result. It also sets interp->freeProc to zero, but
    does not change interp->result or clear error state. Tcl_FreeResult is most
    commonly used when a procedure is about to replace one result value with another.
So, I think you want to use Tcl_ResetResult() here.

Tcl_FreeResult() will be removed in Tcl 9.0, you now know why. See: https://core.tcl-lang.org/tips/doc/trunk/tip/559.md

Remark: The Get_Info() calls have two parameters, but this functions is defined with 3 parameters. I just assume you forgot the first "interp" parameter, without this your code wouldn't work at all.

Log in (anonymous is fine) to add a comment.