Tk Source Code

View Ticket
Login
Ticket UUID: 7ca431c997a71357838cb700024fbb3c01d1fce
Title: MS-Win: tk_getOpenFile and tk_getSaveFile leak memory when -initialfile is given more than once
Type: Bug Version: 9.1b1
Submitter: serhiy.storchaka Created on: 2026-09-24 14:45:29
Subsystem: 37. [tk_get*File] Assigned To: jan.nijtmans
Priority: 5 Medium Severity: Minor
Status: Closed Last Modified: 2026-09-25 07:02:00
Resolution: Fixed Closed By: jan.nijtmans
    Closed on: 2026-09-25 07:02:00
Description:

Since [6b4b9c0d32] (TIP 744) ParseOFNOptions() in win/tkWinDialog.c converts the -initialfile value into the utf16FileName Tcl_DString with Tcl_UtfToExternalDStringEx(). That call re-initializes the DString without freeing it. If -initialfile is given twice, the buffer of the first value is lost when it does not fit into the DString's static space (about 100 characters). -initialdir frees its old value first and is not affected.

The leak shows without opening a dialog, because an invalid option makes the call fail after the -initialfile values are parsed:

set long [string repeat x 1000]
for {set i 0} {$i < 100000} {incr i} {
    catch {tk_getOpenFile -initialfile $long -initialfile x -bogus 1}
}

This loop takes about 400 MB in 9.1b1 and nothing in 9.0.5.

User Comments: jan.nijtmans added on 2026-09-25 07:02:00:

Fixed [ec1a14ce89e33efb|here]

Thanks for the report and the patch!


serhiy.storchaka added on 2026-09-24 16:24:49:

Fixed in branch win-dialog-initialfile-leak ([18ed3e6f01]): the previous -initialfile value is now freed before the new one is converted. The loop above no longer leaks memory, and winDialog.test passes.

There is no automatic test, because a release build cannot measure its own memory use.