Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2024-12-10
10:09 • Closed ticket [b343fe8656]: TIP 636 Tcl_ListObjIndex incompatibility not documen... artifact: 9ebbf9bf0e user: apnadkarni
2024-12-02
09:43 • Ticket [b343fe8656]: 3 changes artifact: 981e54f9b9 user: dkf
2024-11-29
00:03 • Ticket [b343fe8656]: 3 changes artifact: e570077f2f user: apnadkarni
2024-11-28
19:50 • Ticket [b343fe8656]: 3 changes artifact: 0c80b3c4a6 user: griffin
2024-11-27
10:38 • Ticket [b343fe8656]: 4 changes artifact: 854593f533 user: apnadkarni
10:38 • New ticket [b343fe8656]. artifact: 6e989b5abf user: apnadkarni

TIP 636 Tcl_ListObjIndex incompatibility not documented

Closed / Fixedb343fe8656c16b2e02580f68423b6ef03463915cCreated 2 years agoUpdated 2 years ago
Descriptionmarkdown2024-11-27 10:38:08

The Incompatibility section in TIP 636 does not seem to be documented afaict. It should probably to added to the ListObj.3 manpage as Tcl_ListObjIndex is impacted.

apnadkarni Ticket [b343fe8656]: 4 changes [History diff]2024-11-27 10:38:36
griffinmarkdown2024-11-28 19:50:32

See branch [bug-b343fe8656] for a proposed fix.

apnadkarnimarkdown2024-11-29 00:03:53

I think that branch mostly describes what the caller needs to do except for one case.

  1. Tcl_ListObjIndex may return a Tcl_Obj with (a) an internal reference held (non-0 ref count) or (b) a 0 ref count.

  2. The caller may either (a) hold on to the object, (b) simply access the content and dispose of it, or (c) pass it to another function.

The case not described that needs more clarity is 1(b) with 2(c). In that case, I don't think Tcl_BounceRefCount is always safe. In particular if the second function that is called is a "hairy monster" like Tcl_ObjSetVar2, the correct sequence is wrapping that call between a IncrRefCount and DecrRefCount and not Bounce.

But explaining that succinctly is another matter.

A related note - there is also a REFERENCE COUNT MANAGEMENT section in that page.

My suggestion is to merge into the trunk but leave this ticket open as a reminder to clarify the abovementioned case.

dkfmarkdown2024-12-02 09:43:35

The hairiest hair on the monster happens when the called function decrements the reference count of the object back to zero. This definitely happens in Tcl_EvalObj() in some cases, but for Tcl_ObjSetVar2() is only ever likely to be a factor for the keyObj and valueObj arguments if there are traces in play (as those could effectively reject or replace the write).

Background

For untraced variables, the reference count changes are:

  • nameObj: don't increment reference count
  • keyObj: increment reference count only when new array element is allocated
  • valueObj: increment reference count only on successful write

Constants reject writes prior to reference counts being updated, and don't apply to arrays or their elements (because there was a case in there that made my head ache and it was easier to just block safely and let someone else enable when they figure out what a constant array means and how you'd actually make it). This sort of thing is why I made constants a separate thing, despite it being possible to simulate them with traces/links in older versions of Tcl.

Traces typically involve callbacks into the Tcl interpreter with no restrictions, and happen after the write has been done, so reference count updates can go down in that case (e.g., because a whole array trace removes the element you've just made) and full hairiness is achieved; both the key and the value arguments have their reference counts decreased back to their start value before the call to Tcl_ObjSetVar2 exits (i.e., the handles are not safe to use after exit at all).

apnadkarni Closed ticket [b343fe8656]. [History diff]2024-12-10 10:09:12
Log in (anonymous is fine) to add a comment.