|
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
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.
a6e75dd9bbMerge 9.0. Fix [e8b18d7b1f] - release encodings on error check-in2025-06-20 16:05:2277100b263aFix [e8b18d7b1f] - release encodings on error check-in2025-06-20 16:00:234df76c2abdStart on [e8b18d7b1f] fix. check-in2025-06-18 18:12:18
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.
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_FreeEncodingbut 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_GetEncodingFromObjwhich in effect adds an extra reference count for exactly that reason. Even with aTcl_FreeEncodingit will stay in memory until the cachingTcl_Objis 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.
Fixed in [77100b263a].