This is the mail archive of the insight@sources.redhat.com mailing list for the Insight project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [RFA] Sorting symbols. Again.



On Wednesday, January 30, 2002, at 02:37 , Daniel Jacobowitz wrote:

> On Wed, Jan 30, 2002 at 02:01:10PM -0800, Syd Polk wrote:
>
>> You should not have to loop through the objects in list_objv to Incr or
>> Decr their refCounts. Tcl_SetListObj does that automatically. They are
>> created with a refCount of 0; Tcl_SetListObj incrs them to 1. What you
>> are doing is creating them, setting them to 1, calling Tcl_SetListObj
>> (which incrs them to 2), and the decrementing them back to 1.
>>
>> Also, it is really, really slimy to create Tcl_Objs without going
>> through Tcl_New*Obj.
>>
>> I would much prefer that you duplicate the list, and then call "lsort".
>> Pseudo-code:
>>
>> 	Tcl_Obj *commandArray[2];
>>
>> 	Tcl_ListObjGetElements (NULL, result_ptr->obj_ptr, &list_objc,
>> &list_objv);
>> 	newList = Tcl_NewListObj(list_objc, list_objv);
>> 	commandArray[1] = newList;
>> 	Tcl_IncrRefCount(newList);
>> 	commandArray[0] = Tcl_NewObjFromString("lsort");
>> 	Tcl_IncrRefCount(commandArray[0]);
>> 	result = Tcl_EvalObjv(interp, 2, commandArray);
>> 	Tcl_DecrRefCount(commandArray[0]);
>> 	
>> 	/* newList now has sorted list. */
>>
>> I am not a maintainer, but I worked in the core of Tcl for a couple of
>> years for John O., and doing block allocates of Tcl_Objs is just asking
>> for trouble, and will lead to problems if insight is ever compiled to
>> TCL_MEM_DEBUG, or other things.
>
> I don't think I did anything of the sort; but perhaps I misunderstood
> Tcl_SetListObj.  The documentation doesn't say anything about how to
> allocate the objv.  The refcount mess was because of using SetListObj,
> but I suppose I understand now that creating a new object and then
> destroying the old one would have worked out cleaner.  Hopefully the
> above will be accepted and I can delete the sort entirely :)
>

+      Tcl_ListObjGetElements (NULL, result_ptr->obj_ptr, &list_objc, 
&list_objv);
+      new_objv = (Tcl_Obj **) malloc (sizeof (Tcl_Obj *) * list_objc);
+      memcpy (new_objv, list_objv, sizeof (Tcl_Obj *) * list_objc);

This memcpy duplicates Tcl_Obj's without going through approved API. 
This can screw up refcounting of the individual Tcl_Obj's in the list.

> Thanks for the helpful comments.
>
> --
> Daniel Jacobowitz                           Carnegie Mellon University
> MontaVista Software                         Debian GNU/Linux Developer
>
>
Syd Polk
QA and Integration Manager, Mac OS X Development Tools
+1 408 974-0577


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]