Skip to content
Tcl Source Code Artifact [580bed3b76]
Login

Artifact [580bed3b76]

Artifact 580bed3b76b2b1740917983c15864c2d38e00ccab67f27885f4a4969f4b08b10:



CommandName: unload ManualSection: n Version: 8.5 TclPart: Tcl TclDescription: Tcl Built-In Commands Links:

Name

unload - Unload machine code

Synopsis

::: {.synopsis} ::: [unload]{.cmd} [switches]{.optarg} [fileName]{.arg} [unload]{.cmd} [switches]{.optarg} [fileName]{.arg} [prefix]{.arg} [unload]{.cmd} [switches]{.optarg} [fileName]{.arg} [prefix]{.arg} [interp]{.arg} :::

Description

This command tries to unload shared libraries previously loaded with fBloadfR from the application's address space. fIfileNamefR is the name of the file containing the library file to be unload; it must be the same as the filename provided to fBloadfR for loading the library. The fIprefixfR argument is the prefix (as determined by or passed to fBloadfR), and is used to compute the name of the unload procedure; if not supplied, it is computed from fIfileNamefR in the same manner as fBloadfR. The fIinterpfR argument is the path name of the interpreter from which to unload the package (see the fBinterpfR manual entry for details); if fIinterpfR is omitted, it defaults to the interpreter in which the fBunloadfR command was invoked.

If the initial arguments to fBunloadfR start with fB-fR then they are treated as switches. The following switches are currently supported:

-nocomplain : Suppresses all error messages. If this switch is given, fBunloadfR will never report an error.

-keeplibrary : This switch will prevent fBunloadfR from issuing the operating system call that will unload the library from the process.

-|- : Marks the end of switches. The argument following this one will be treated as a fIfileNamefR even if it starts with a fB-fR.

Unload operation

When a file containing a shared library is loaded through the fBloadfR 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 while the second describes how many times the library has been loaded into safe interpreters. As a file containing a shared library can be loaded only once by Tcl (with the first fBloadfR call on the file), these counters track how many interpreters use the library. Each subsequent call to fBloadfR after the first simply increments the proper reference count.

unload works in the opposite direction. As a first step, fBunloadfR will check whether the library is unloadable: an unloadable library exports a special unload procedure. The name of the unload procedure is determined by fIprefixfR and whether or not the target interpreter is a safe one. For normal interpreters the name of the initialization procedure will have the form fIpfxfRfB_UnloadfR, where fIpfxfR is the same as fIprefixfR except that the first letter is converted to upper case and all other letters are converted to lower case. For example, if fIprefixfR is fBfoofR or fBFOofR, the initialization procedure's name will be fBFoo_UnloadfR. If the target interpreter is a safe interpreter, then the name of the initialization procedure will be fIpkgfRfB_SafeUnloadfR instead of fIpkgfRfB_UnloadfR.

If fBunloadfR determines that a library is not unloadable (or unload functionality has been disabled during compilation), an error will be returned. If the library is unloadable, then fBunloadfR will call the unload procedure. If the unload procedure returns fBTCL_OKfR, fBunloadfR 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.

Unload hook prototype

The unload procedure must match the following prototype:

typedef int Tcl_LibraryUnloadProc(
        Tcl_Interp *interp,
        int flags);

The fIinterpfR argument identifies the interpreter from which the library is to be unloaded. The unload procedure must return fBTCL_OKfR or fBTCL_ERRORfR to indicate whether or not it completed successfully; in the event of an error it should set the interpreter's result to point to an error message. In this case, the result of the fBunloadfR command will be the result returned by the unload procedure.

The fIflagsfR argument can be either fBTCL_UNLOAD_DETACH_FROM_INTERPRETERfR or fBTCL_UNLOAD_DETACH_FROM_PROCESSfR. 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), fBTCL_UNLOAD_DETACH_FROM_INTERPRETERfR 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 fIflagsfR argument will be set to fBTCL_UNLOAD_DETACH_FROM_PROCESSfR.

Notes

The fBunloadfR command cannot unload libraries that are statically linked with the application. If fIfileNamefR is an empty string, then the fIprefixfR argument must be specified.

If fIprefixfR 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 to take the last element of fIfileNamefR, strip off the first three characters if they are fBlibfR, then strip off the next three characters if they are fBtcl9fR, and use any following wordchars but not digits, converted to titlecase as the prefix. For example, the command fBunload libxyz4.2.sofR uses the prefix fBXyzfR and the command fBunload bin/last.so {}fR uses the prefix fBLastfR.

Portability issues

Unix : Not all unix operating systems support library unloading. Under such an operating system fBunloadfR returns an error (unless fB-nocomplainfR has been specified).

Bugs

If the same file is fBloadfRed by different fIfileNamefRs, 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). 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 fBunloadfR on such a library (as the library will be completely detached from the application while some interpreters will continue to use it).

Example

If an unloadable module in the file fBfoobar.dllfR had been loaded using the fBloadfR 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.