unload(3tcl)
π unload β the Tcl built-in command that yeets machine code back out π
okay bestie, welcome to the unload(3tcl) page, straight outta the Tcl Built-In Commands collection β¨ this one's the reverse glow-up of load, and it's lowkey iconic.
β¨ SYNOPSIS β¨
unload ?switches? fileName
unload ?switches? fileName prefix
unload ?switches? fileName prefix interp
πΈ DESCRIPTION πΈ
So here's the vibe: this command tries to unload shared libraries that were previously loaded with load, pulling them right back out of the application's address space. bye girl π
fileName is the name of the file containing the library file to be unload β and yes, it must be the same as the filename provided to load when you loaded it. no swapsies, no nicknames, the exact same name fr fr.
prefix is the prefix (as determined by or passed to load), and it gets used to compute the name of the unload procedure. if you don't supply it, it's computed from fileName in the same manner as load does. π
interp is the path name of the interpreter from which to unload the package (peep the interp manual entry for the details, iykyk). if interp is omitted, it defaults to the interpreter in which the unload command was invoked. main character energy, honestly. π
If the initial arguments to unload start with - then they're treated as switches. here's what's currently supported:
-nocomplainβ suppresses all error messages. if this switch is given,unloadwill never report an error. zero drama, zero receipts π-keeplibraryβ this switch preventsunloadfrom issuing the operating system call that would unload the library from the process. it's giving "we stay." π«Ά--β marks the end of switches. the argument following this one will be treated as afileNameeven if it starts with a-. protective queen behavior π‘οΈ
π§ UNLOAD OPERATION π§
okay this part is the β¨loreβ¨ so pay attention.
When a file containing a shared library is loaded through the load command, Tcl associates two reference counts to the library file:
- the first counter shows how many times the library has been loaded into normal (trusted) interpreters
- the second describes how many times the library has been loaded into safe interpreters
since a file containing a shared library can be loaded only once by Tcl (with the first load call on the file), these counters track how many interpreters use the library. every subsequent call to load after the first simply increments the proper reference count. it's a whole little tally system, obsessed. π
unload works in the opposite direction. as a first step, unload checks whether the library is unloadable: an unloadable library exports a special unload procedure. the name of that unload procedure is determined by prefix and whether or not the target interpreter is a safe one.
- for normal interpreters, the name of the initialization procedure will have the form
pfx_Unload, wherepfxis the same asprefixexcept that the first letter is converted to upper case and all other letters are converted to lower case. for example, ifprefixisfooorFOo, the initialization procedure's name will beFoo_Unload. β¨ - if the target interpreter is a safe interpreter, then the name of the initialization procedure will be
pkg_SafeUnloadinstead ofpkg_Unload.
If unload determines that a library is not unloadable (or unload functionality has been disabled during compilation), an error will be returned π. but if the library is unloadable, then unload will call the unload procedure. if the unload procedure returns TCL_OK, unload will proceed and decrease the proper reference count (depending on the target interpreter type). when both reference counts have reached 0, the library will be detached from the process. periodt. π
π¦ UNLOAD HOOK PROTOTYPE π¦
the unload procedure must match the following prototype, no cap:
typedef int Tcl_LibraryUnloadProc(
Tcl_Interp *interp,
int flags);
the interp argument identifies the interpreter from which the library is to be unloaded. the unload procedure must return TCL_OK or TCL_ERROR to indicate whether or not it completed successfully β and in the event of an error it should set the interpreter's result to point to an error message. in that case, the result of the unload command will be the result returned by the unload procedure. π¬
the flags argument can be either TCL_UNLOAD_DETACH_FROM_INTERPRETER or TCL_UNLOAD_DETACH_FROM_PROCESS. in case the library will remain attached to the process after the unload procedure returns (i.e. because the library is used by other interpreters), TCL_UNLOAD_DETACH_FROM_INTERPRETER will be defined. however, if the library is used only by the target interpreter and the library will be detached from the application as soon as the unload procedure returns, the flags argument will be set to TCL_UNLOAD_DETACH_FROM_PROCESS. π
π NOTES π
- the
unloadcommand cannot unload libraries that are statically linked with the application. sorry babe, that's just how it is π - if
fileNameis an empty string, then theprefixargument must be specified.
if prefix is omitted or specified as an empty string, Tcl tries to guess the prefix. this may be done differently on different platforms. the default guess, which is used on most UNIX platforms, is:
- take the last element of
fileName - strip off the first three characters if they are
lib - then strip off the next three characters if they are
tcl9 - and use any following wordchars but not digits, converted to titlecase, as the prefix
for example, the command unload libxyz4.2.so uses the prefix Xyz and the command unload bin/last.so {} uses the prefix Last. π
π PORTABILITY ISSUES π
- Unix β not all unix operating systems support library unloading. under such an operating system
unloadreturns an error (unless-nocomplainhas been specified). so like, check before you commit, bestie.
π BUGS π
If the same file is loaded by different fileNames, it will be loaded into the process's address space multiple times. the behavior of this varies from system to system (some systems may detect the redundant loads, others may not). not me side-eyeing every platform differently π
In case a library has been silently detached by the operating system (and as a result Tcl thinks the library is still loaded), it may be dangerous to use unload on such a library (as the library will be completely detached from the application while some interpreters will continue to use it). this is the actual scary part, do not play with it. π¨
π EXAMPLE π
If an unloadable module in the file foobar.dll had been loaded using the load command like this (on Windows):
load c:/some/dir/foobar.dll
then it would be unloaded like this:
unload c:/some/dir/foobar.dll
This allows a C code module to be installed temporarily into a long-running Tcl program and then removed again (either because it is no longer needed or because it is being updated with a new version) without having to shut down the overall Tcl process. iconic behavior honestly, temporary guests only β¨
π«Ά SEE ALSO π«Ά
info sharedlibextensionload(3tcl)safe(3tcl)
π KEYWORDS π
binary code, unloading, safe interpreter, shared library
and that's the whole page, bestie π
this was Tcl 8.5, unload(3tcl) β go forth and detach responsibly, mwah ππβ¨
π¦ where this page comes from
- debian trixie: tcl9.0-doc 9.0.1+dfsg-2
- ubuntu questing: tcl9.0-doc 9.0.2+dfsg-1
- ubuntu resolute: tcl9.0-doc 9.0.3+dfsg-1
- ubuntu stonking: tcl9.0-doc 9.0.4+dfsg-1
- Copyright (c) 2003 George Petasis <petasis@iit.demokritos.gr>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms