NAME
TclPreserve, TclRelease, TclEventuallyFree - avoid freeing storage
while it's being usedSYNOPSIS
##iinncclluuddee <
TTccllPPrreesseerrvvee(clientData) TTccllRReelleeaassee(clientData) TTccllEEvveennttuuaallllyyFFrreeee(clientData, freeProc) AARRGGUUMMEENNTTSS ClientData clientData (in) Token describing structure to be freed or reallocated. Usually a pointer to memory for structure. TclFreeProc *freeProc (in) Procedure to invoke to free clientData.> DESCRIPTION
These three procedures help implement a simple reference count mecha-
nism for managing storage. They are designed to solve a problem havingto do with widget deletion, but are also useful in many other situa-
tions. 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's 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 causesan event-handling C procedure in the button to be invoked, which in
turn causes the button's associated Tcl command to be executed, whichin turn causes the button to be deleted, which in turn causes the but-
ton'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 retaineduntil the event-handling procedure has finished with it. In other sit-
uations where the widget is deleted, it may be possible to free the widget record immediately.TTccllPPrreesseerrvvee and TTccllRReelleeaassee implement short-term reference counts for
their clientData argument. The clientData argument identifies anobject and usually consists of the address of a structure. The refer-
ence counts guarantee that an object will not be freed until each call to TTccllPPrreesseerrvvee for the object has been matched by calls to TTccllRReelleeaassee. There may be any number of unmatched TTccllPPrreesseerrvvee calls in effect at once. TTccllEEvveennttuuaallllyyFFrreeee is invoked to free up its clientData argument. It checks to see if there are unmatched TTccllPPrreesseerrvvee calls for the object. If not, then TTccllEEvveennttuuaallllyyFFrreeee calls freeProc immediately. Otherwise TTccllEEvveennttuuaallllyyFFrreeee records the fact that clientData needs eventually to be freed. When all calls to TTccllPPrreesseerrvvee have been matched with calls to TTccllRReelleeaassee then freeProc will be called by TTccllRReelleeaassee to do the cleanup.All the work of freeing the object is carried out by freeProc. FreeP-
roc must have arguments and result that match the type TTccllFFrreeeePPrroocc: typedef void TclFreeProc(char *blockPtr); The blockPtr argument to freeProc will be the same as the clientDataargument to TTccllEEvveennttuuaallllyyFFrreeee. The type of blockPtr (cchhaarr **) is dif-
ferent than the type of the clientData argument to TTccllEEvveennttuuaallllyyFFrreeee for historical reasons, but the value is the same. When the clientData argument to TTccllEEvveennttuuaallllyyFFrreeee refers to storage allocated and returned by a prior call to TTccllAAlllloocc, cckkaalllloocc, or another function of the Tcl library, then the freeProc argument should be given the special value of TTCCLLDDYYNNAAMMIICC. This mechanism can be used to solve the problem described above by placing TTccllPPrreesseerrvvee and TTccllRReelleeaassee calls around actions that maycause 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.SEE ALSO
TclInterp, TclAlloc KKEEYYWWOORRDDSS free, reference count, storage Tcl 7.5 TclPreserve(3)