
if {0} {
Hello !!

Although I read the wiki/doc about Tcl_Obj Incr/DecrRefCount , there are issues I still don't understand, notably when should I delete Tcl_Obj when there RefCount changed. I comment the code below as I understand how ref counts are managed by public Tcl routines.
My problem with this code is that the memory keeps increading as the routine is called several times

(as a help for understanding the code, objv[1] contains a list made of pairs : name1 seq1 mane2 seq2 ... all seqX have the same length)

  Tcl_Obj **Lseq, *Res, *LSG, *LGS, *ONom, *Og, *Os;

  if (Tcl_ListObjGetElements(interp,objv[1],&nseq,&Lseq) != TCL_OK) {
    /* Lseq has refCount + 1 */
    /* Lseq can't be deleted , as said in the man page :"The memory pointed to is managed by Tcl and should not be freed or written to by the caller" */
    return TCL_ERROR;
  }

  LSG = Tcl_NewListObj(0,NULL);
  LGS = Tcl_NewListObj(0,NULL);

  /* catch the sequence length */
  sq0 = Tcl_GetByteArrayFromObj(Lseq[1],&slen);
  seq = (char *)ckalloc((slen+1) * sizeof(char));

  /* Treat each pair of name/seq */
  for (i=0;i<nseq;i+=2) {
    ONom= Tcl_NewStringObj(Tcl_GetString(Lseq[i]),-1);
    seq = Tcl_GetByteArrayFromObj(Lseq[i+1],&slen);
    
    g = s = -1;
    Os = Tcl_NewIntObj(0);
    /* loop over the seq g is general, s is seq pos */
    for (j=0;j<slen;j++) {
      g++;
      Og = Tcl_NewIntObj(g);
      if (seq[j] != '.') {
	s++;
	Os = Tcl_NewIntObj(s);
	Tcl_ListObjAppendElement(interp, LSG, ONom);
	Tcl_ListObjAppendElement(interp, LSG, Os);
	Tcl_ListObjAppendElement(interp, LSG, Og);
      }
      Tcl_ListObjAppendElement(interp, LGS, ONom);
      Tcl_ListObjAppendElement(interp, LGS, Og);
      Tcl_ListObjAppendElement(interp, LGS, Os);
    }
    /* 
       At this point, ONom Og Os have refCount + 1 
    */

    /* output sequence length */
    s++;
    Tcl_ListObjAppendElement(interp, LSG, ONom);
    Tcl_ListObjAppendElement(interp, LSG, Tcl_NewIntObj(-1));
    Tcl_ListObjAppendElement(interp, LSG, Tcl_NewIntObj(s));
    Tcl_ListObjAppendElement(interp, LGS, ONom);
    Tcl_ListObjAppendElement(interp, LGS, Tcl_NewIntObj(-1));
    Tcl_ListObjAppendElement(interp, LGS, Tcl_NewIntObj(s));
  }

  Res = Tcl_NewListObj(0,NULL);
  Tcl_ListObjAppendElement(interp,Res,LGS);
  Tcl_ListObjAppendElement(interp,Res,LSG);
  /* LGS and LSG have here refCount + 1 */  

  Tcl_SetObjResult(interp,Res);
  /* As interpreter result is pointing to Res, Res ref count is incremented.
     Res should be released as soon as used
  */

  //ckfree((char *) seq);

  return TCL_OK;

}

this extension is used like :
lassign [MyExt [array names Seq]] L1 L2

Many thanks for enlighting me !!

Luc

