tclswig

Tcl and SWIG carrays.i memory usage


I use SWIG carrays.i library to create wrapper to double arrays access. If I add this to interface file: %array_functions(double, doubleArray);, I get next function available in tcl: new_doubleArray, delete_doubleArray, doubleArray_setitem and doubleArray_getitem. My question is: if create array with new_doubleArray command inside tcl procedure (testProc), should I delete it explicitly with delete_doubleArray before return from procedure? By tcl logic all local variables deleted after procedure exit, but I am not sure about SWIG arrays. Th example of the code is:

proc createArray {list} {
    set length [llength $list]
    set a [new_doubleArray $length]
    for {set i 0} {$i<$length} {incr i} {
        set iElem [@ $list $i]
        if {[string is double -strict $iElem]==0} {
            error "Element of list must be of type double"
        }
        doubleArray_setitem $a $i $iElem
    }
    return $a
}

proc testProc {list} {
    set listArray [createArray $list]
    return 
}

Thank you in advance.


Solution

  • Yes, SWIG is allocating memory on the heap via the new_doubleArray and it must be freed explicitly via delete_doubleArray.