Tcl_Preserve, Tcl_Release, Tcl_EventuallyFree − avoid freeing storage while it is being used


#include <tcl.h>



Tcl_EventuallyFree(clientData, freeProc)


ClientData clientData (in)

Token describing structure to be freed or reallocated. Usually a pointer to memory for structure.

Tcl_FreeProc *freeProc (in)

Procedure to invoke to free clientData.



These three procedures help implement a simple reference count mechanism for managing storage. They are designed to solve a problem having to do with widget deletion, but are also useful in many other situations. When a widget is deleted, its widget record (the structure holding information specific to the widget) must be returned to the storage allocator. However, it is possible that the widget record is in active use by one of the procedures on the stack at the time of the deletion. This can happen, for example, if the command associated with a button widget causes the button to be destroyed: an X event causes an event-handling C procedure in the button to be invoked, which in turn causes the button’s associated Tcl command to be executed, which in turn causes the button to be deleted, which in turn causes the button’s widget record to be de-allocated. Unfortunately, when the Tcl command returns, the button’s event-handling procedure will need to reference the button’s widget record. Because of this, the widget record must not be freed as part of the deletion, but must be retained until the event-handling procedure has finished with it. In other situations where the widget is deleted, it may be possible to free the widget record immediately.

Tcl_Preserve and Tcl_Release implement short-term reference counts for their clientData argument. The clientData argument identifies an object and usually consists of the address of a structure. The reference counts guarantee that an object will not be freed until each call to Tcl_Preserve for the object has been matched by calls to Tcl_Release. There may be any number of unmatched Tcl_Preserve calls in effect at once.

Tcl_EventuallyFree is invoked to free up its clientData argument. It checks to see if there are unmatched Tcl_Preserve calls for the object. If not, then Tcl_EventuallyFree calls freeProc immediately. Otherwise Tcl_EventuallyFree records the fact that clientData needs eventually to be freed. When all calls to Tcl_Preserve have been matched with calls to Tcl_Release then freeProc will be called by Tcl_Release to do the cleanup.

All the work of freeing the object is carried out by freeProc. FreeProc must have arguments and result that match the type Tcl_FreeProc:

typedef void Tcl_FreeProc(char *blockPtr);

The blockPtr argument to freeProc will be the same as the clientData argument to Tcl_EventuallyFree. The type of blockPtr (char *) is different than the type of the clientData argument to Tcl_EventuallyFree for historical reasons, but the value is the same.

When the clientData argument to Tcl_EventuallyFree refers to storage allocated and returned by a prior call to Tcl_Alloc, ckalloc, or another function of the Tcl library, then the freeProc argument should be given the special value of TCL_DYNAMIC.

This mechanism can be used to solve the problem described above by placing Tcl_Preserve and Tcl_Release calls around actions that may cause undesired storage re-allocation. The mechanism is intended only for short-term use (i.e. while procedures are pending on the stack); it will not work efficiently as a mechanism for long-term reference counts. The implementation does not depend in any way on the internal structure of the objects being freed; it keeps the reference counts in a separate structure.


Tcl_Interp, Tcl_Alloc


free, reference count, storage

More Linux Commands

smidiff(1) - check differences between a pair of SMI or SPPI
The smidiff program is used to check differences between a pair of SMI MIB modules or SPPI PIB modules. E.g., it can be used to detect changes in updated MIB mo

endian(3) - convert values between host and big-/little-endi
These functions convert the byte encoding of integer values from the byte order that the current CPU (the host) uses, to and from little-endian and big-endian b

gencat(1) Generate message catalog - Linux manual page......
The gencat program is specified in the X/Open standard and the GNU implementation follows this specification and so processes all correctly formed input files.

sane-sp15c(5) - SANE backend for Fujitsu ScanPartner 15C fla
The sane-sp15c library implements a SANE (Scanner Access Now Easy) backend which provides access to the Fujitsu flatbed scanners. At present, the following scan

XtNew(3) - memory management functions - Linux manual page
The XtMalloc functions returns a pointer to a block of storage of at least the specified size bytes. If there is insufficient memory to allocate the new block,

labelframe(n) - Create and manipulate labelframe widgets....
The labelframe command creates a new window (given by the pathName argument) and makes it into a labelframe widget. Additional options, described above, may be

innmail(1) - Simple mail-sending program - Linux man page...
innmail is a Perl script intended to provide the non-interactive mail-sending functionality of mail(1) while avoiding nasty security problems. It takes the body

jpgicc(1) little cms ICC profile applier for JPEG...........
jpgicc.1 - lcms is a standalone CMM engine, which deals with the color management. It implements a fast transformation between ICC profiles. jpegicc is little c

lmtpd(8) - LMTP server process (Admin - Linux man page).....
Lmtpd is an LMTP server used to deliver mail to the IMAP mailstore. It accepts commands on its standard input and responds on its standard output. It MUST be in

namespace.conf(5) - the namespace configuration file........
The module allows setup of private namespaces with polyinstantiated directories. Directories can be polyinstantiated based on user name or, in

XpSelectInput(3x) - Specifies which X Print events, from the
XpSelectInput selects which X Print events from the specified print context the client is interest in. The X Print Events are generated from a current print con

field_type(3form) - data type validation for fields.........
The function set_field_type declares a data type for a given form field. This is the type checked by validation functions. The predefined types are as follows:

We can't live, work or learn in freedom unless the software we use is free.