Skip to content
Tcl Source Code View Ticket
Login

View Ticket

Clarify that [apply] functions are cleaned up

Closed / Invalid0e48e053283e6fb8dcc4ecd183539ae57a31072bCreated 9 years agoUpdated 42 days ago
Descriptionplain2018-03-26 16:21:37
The manual page for the [apply] command describes the semantics of the command with a pure Tcl procedure. However, that implementation would never clean up created procedures, leading to memory leaks where the [apply] command is parameterised in, say, a loop. It would be good to mention that, unlike the pure Tcl description, [apply] commands are cleaned up with the reference count system just as with any other Tcl values.

Alternatively the semantics code could be altered to destroy the newly created command before [return].
sebreswiki2018-03-26 20:02:35

1. If you meant the interim proc "$name" created inside example in the manual page - it will be already removed. Possibly you overlooked one thing, the example does additionally - the clean-up with "rename [lindex [info level 0] 0] {}", which deletes itself implicitly before it returns from each "apply-proc".

2. In part where the manual page describes the semantics "with a pure Tcl procedure" it meant just really semantic only - so it is similar as it acts like this proc.

So if you meant any other case, like create another proc inside... The case where something evaluable (inclusive the command "apply") creates a "sub" proc inside of its body, is equivalent to any other script-level bodies where proc will be created (tcl does not have scope-bound proc's at all, so never implicit removed).

So it means - the proc's created from everywhere are always persistent (until removed using `rename $name {}`. And body of apply-lambda does not different compared to any other evaluated tcl-body.

This is not a leak (despite the proc inside will be normally always overwritten, if same name used).

So in following example both are almost equivalent:


proc test {a b} {
  proc log_params {args} {puts "execute [info level [info level]]"}
  log_params a $a b $b
}
test A B

set lambda {{a b} { proc log_params {args} {puts "execute [info level [info level]]"} log_params a $a b $b }} apply $lambda A B

In both cases the proc is created inside body (note it's compiled only once because of tcl-jit-compiler).

Thus could you explain firstly which of both cases did you meant?

serhiy.storchakawiki2026-10-15 11:29:05

Closing: answered above.

serhiy.storchaka Ticket [0e48e05328]: 3 changes [History diff]2026-10-22 17:19:26
Log in (anonymous is fine) to add a comment.