Sujet : Re: try... query
De : auriocus (at) *nospam* gmx.de (Christian Gollwitzer)
Groupes : comp.lang.tclDate : 06. Dec 2024, 18:37:25
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vivcol$2feip$1@dont-email.me>
References : 1
User-Agent : Mozilla Thunderbird
Am 06.12.24 um 14:11 schrieb Alan Grunwald:
I'm attempting to get my head around preventing resource leaks, using try and return.
I haven't worked through your code, but in C++, there is a great idiom called RAII - things get destroyed when the variable goes out of scope. This can be simulated for Tcl using a delete trace on the variable.
Using the little utils package from here:
https://github.com/BessyHDFViewer/BessyHDFViewer/tree/main/bessyhdfviewer.vfs/lib/SmallUtilsYou can do this for files:
proc something {} {
SmallUtils::autofd fd somewfile w
puts $fd "Text comes here"
}
# fd goes out of scope, file is closed
and for any objects that work Tk-like, giving a command back:
SmallUtils::autovar btn button .b -text "Hi"
# will destroy if btn goes out of scope
# by "rename .b {}"
Of course, this is not perfect, it will fail if you copy the variable somewhere else, but often times it is good enough.
Christian