Clarify that [apply] functions are cleaned up
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].
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?
Closing: answered above.