Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2025-06-20
16:09 • Closed ticket [e8b18d7b1f]: EncodingConvert{to,from}ObjCmd do not free Tcl_Encod... artifact: 52c2b98e84 user: apnadkarni
16:05
Merge 9.0. Fix [e8b18d7b1f] - release encodings on error check-in: a6e75dd9bb user: apnadkarni tags: trunk, main
16:00
Fix [e8b18d7b1f] - release encodings on error check-in: 77100b263a user: apnadkarni tags: core-9-0-branch
2025-06-18
18:12
Start on [e8b18d7b1f] fix. closed check-in: 4df76c2abd user: apnadkarni tags: bug-e8b18d7b1f
14:08 • Ticket [e8b18d7b1f] EncodingConvert{to,from}ObjCmd do not free Tcl_Encoding on e... artifact: 41c40a341b user: apnadkarni
12:52 • Ticket [e8b18d7b1f]: 3 changes artifact: 008e1f9d01 user: dgp
08:00 • Ticket [e8b18d7b1f]: 3 changes artifact: d3ea537824 user: apnadkarni
07:59 • New ticket [e8b18d7b1f]. artifact: e3b4cb9856 user: apnadkarni

EncodingConvert{to,from}ObjCmd do not free Tcl_Encoding on error paths

Closed / Fixede8b18d7b1fbe160a09d9d7dad382a3d150b41c87Created 1 year agoUpdated 1 year ago
Descriptionplain2025-06-18 07:59:59
Both the mentioned functions have error paths that fail to call Tcl_EncodingFree on the retrieved Tcl_Encoding.

The Tcl_Encoding structures are reference counted so there is no observable memory growth no matter how many times the error path is taken hence marked as Minor. But the resources are effectively never freed if there are encoding errors even if the encoding is no longer needed.
Referenced by3
apnadkarni Ticket [e8b18d7b1f]: 3 changes [History diff]2025-06-18 08:00:12
dgpplain2025-06-18 12:52:30
Just a word of caution.  I'd have to refresh to get the details right
but I believe there is some care taken to avoid release of Tcl_Encoding
because many of them have to be brought in from disk, and a program that
gets in a cycle of load-use-discard will not be as happy as if it just
holds on to a loaded encoding it is likely to use again.
apnadkarnimarkdown2025-06-18 14:08:38

Appreciate the warning but I am indeed aware of the loading and caching mechanism for encodings. However, note that

  • The success case does in fact call Tcl_FreeEncoding but the error case does not. This inconsistency does not make sense if the goal is to keep encodings in memory.

  • The functions in question actually use Tcl_GetEncodingFromObj which in effect adds an extra reference count for exactly that reason. Even with a Tcl_FreeEncoding it will stay in memory until the caching Tcl_Obj is freed or shimmered. So for instance when the encoding name is within a proc, it will effectively keep in the encoding in memory as long as the proc's bytecode exists.

  • As an aside, there is a non-trivial amount of code that deals with caching of encoding with a shared table and mutex access. So I presume at some point it was thought to be important to discard encoding tables. I am not sure what was considered a lot of memory three decades ago is true anymore and at some point it might be worth looking at never releasing loaded encodings, thereby both simplifying the code and more efficient encoding operations by avoiding the locks and cache flushes.

apnadkarniplain2025-06-20 16:09:56
Fixed in [77100b263a].
Log in (anonymous is fine) to add a comment.