Tk Source Code

View Ticket
Login
Ticket UUID: 0a62b5450b22b353831879c2bfd997161cc11cb2
Title: Leaks involving TIP 529 Tcl_DictObjGet() usage
Type: Bug Version: 9.0.0
Submitter: chrstphrchvz Created on: 2026-11-08 23:58:00
Subsystem: 41. Photo Images Assigned To: nobody
Priority: 5 Medium Severity: Minor
Status: Closed Last Modified: 2026-11-11 09:55:51
Resolution: Fixed Closed By: oehhar
    Closed on: 2026-11-11 09:55:51
Description:

In WriteExtraChunks() (tkImgPNG.c):

	if (TCL_ERROR == Tcl_DictObjGet(interp, metadataInObj,
		Tcl_NewStringObj("aspect",-1),
		&aspectObj) ||
	    TCL_ERROR == Tcl_DictObjGet(interp, metadataInObj,
		Tcl_NewStringObj("DPI",-1),
		&DPIObj) ) {
	    return TCL_ERROR;
	}

In CommonWriteGIF() (tkImgGIF.c):

	if (TCL_ERROR == Tcl_DictObjGet(interp, metadataInObj,
		Tcl_NewStringObj("comment",-1),
		&itemData)) {
	    return TCL_ERROR;
	}

The key values created with Tcl_NewStringObj() are leaked. But what should be done about this sort of leak? Is Tcl_DictObjGet() failing to support idiomatic code, or is this not a correct idiom?

User Comments: oehhar added on 2026-11-11 09:55:51:

Thanks. Merged to:

  • main [7cf75fe8]
  • core-9-0-branch [ba8aefcc]

Thanks Nicolas!

Bug closed.

Harald


chrstphrchvz added on 2026-11-11 01:05:27:

[005d9734] seems fine.


oehhar added on 2026-11-09 13:33:00:

What do you think of [ef45dc74ff506f7c] ? Now, keys and also values are freed.

Thanks for all, Harald


oehhar added on 2026-11-09 11:44:07:

Right you are !!!


chrstphrchvz added on 2026-11-09 11:30:34:

The leak occurred regardless of the return value of Tcl_DictObjGet(). So I would think something like this is needed instead, but others may find it even less idiomatic:

	commentObj = Tcl_NewStringObj("comment",-1);
	int status = Tcl_DictObjGet(interp, metadataInObj,
		commentObj,
		&itemData);
	Tcl_BounceRefCount(commentObj);
	if (TCL_ERROR == status) {
	    return TCL_ERROR;
	}

oehhar added on 2026-11-09 10:45:36:

I have written this code.

So, the proposal is: - to fist create the string objects - then add it to the dict - on error free the string object

	commentObj = Tcl_NewStringObj("comment",-1);
	if (TCL_ERROR == Tcl_DictObjGet(interp, metadataInObj,
		commentObj,
		&itemData)) {
	    Tcl_BounceRefCount(commentObj);
	    return TCL_ERROR;
	}

That looks good...

Thanks, Harald