Tcl_Preserve(3)
π Tcl_Preserve β the manual, but make it make sense π
okay bestie, this is a page from the Tcl Library Procedures manual, and it's about Tcl_Preserve, Tcl_Release, and Tcl_EventuallyFree. It's giving "don't you dare free my memory while I'm still using it" energy, and honestly? iconic. π«Ά
β¨ NAME β¨
Tcl_Preserve, Tcl_Release, Tcl_EventuallyFree β the trio that stops you from freeing storage while it's still being used. no cap.
πΈ SYNOPSIS πΈ
#include <tcl.h>
Tcl_Preserve(clientData)
Tcl_Release(clientData)
Tcl_EventuallyFree(clientData, freeProc)
π ARGUMENTS π
void *clientData(in) β the token that describes the structure you're gonna free or reallocate. usually it's just a pointer to the memory for the structure. it's the little name tag for your object. π·οΈTcl_FreeProc *freeProc(in) β the procedure you invoke to freeclientData. that's the one who actually does the deed.
π DESCRIPTION π
so these three procedures help you implement a simple reference count mechanism for managing storage. they were designed to solve a problem that's all about widget deletion, but bestie, they're useful in a whole bunch of other situations too. π
here's the situation. when a widget gets deleted, its widget record β that's the structure holding all the info specific to that widget β has to be returned to the storage allocator. but plot twist: it's totally possible that the widget record is still in active use by one of the procedures sitting on the stack at the exact moment of deletion. π
and like, this happens for real. for example: if the command associated with a button widget causes the button to be destroyed. so an X event causes an event-handling C procedure in the button to be invoked, which causes the button's associated Tcl command to be executed, which causes the button to be deleted, which causes the button's widget record to be de-allocated. π΅βπ«
but unfortunately β and this is the messy part β when the Tcl command returns, the button's event-handling procedure is gonna need to reference the button's widget record. so because of that, the widget record must not be freed as part of the deletion. it has to 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. it's giving "it depends" π
soTcl_PreserveandTcl_Releaseimplement short-term reference counts for theirclientDataargument. theclientDataargument 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 toTcl_Preservefor the object has been matched by calls toTcl_Release. and there may be any number of unmatchedTcl_Preservecalls in effect at once. stack 'em up, girl. π
now Tcl_EventuallyFree is the one you invoke to free up its clientData argument. here's what it does: it checks to see if there are unmatched Tcl_Preserve calls for the object.
- if there are not β then
Tcl_EventuallyFreecallsfreeProcimmediately. done, slay. β¨ - if there are β then
Tcl_EventuallyFreerecords the fact thatclientDataneeds eventually to be freed. and when all calls toTcl_Preservehave been matched with calls toTcl_Release, thenfreeProcwill be called byTcl_Releaseto do the cleanup. it's the group project that actually gets finished. π«Ά
all the work of freeing the object is carried out by freeProc. and freeProc must have arguments and result that match the type Tcl_FreeProc:
typedef void Tcl_FreeProc(
void *blockPtr);
the blockPtr argument to freeProc will be the same as the clientData argument to Tcl_EventuallyFree. same girl, different name tag. π
when theclientDataargument toTcl_EventuallyFreerefers to storage allocated and returned by a prior call toTcl_Allocor another function of the Tcl library, then thefreeProcargument should be given the special value ofTCL_DYNAMIC. don't forget it, bestie, that's the shortcut. π
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.
but real talk β 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. so don't get any ideas. π
and 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. main character energy: minding its own business, keeping receipts elsewhere. π
π SEE ALSO π
Tcl_InterpTcl_Alloc
π KEYWORDS π
free, reference count, storage
and that's the whole vibe check, bestie β this page came straight out of the Tcl manual, version 7.5, dated Tcl_Preserve(3). go forth and preserve your storage responsibly. periodt. πβ¨
π¦ where this page comes from
- alpine 3.23: tcl9-doc 9.0.3-r0 Β· TCL
- alpine 3.24: tcl9-doc 9.0.3-r2 Β· TCL
- fedora 43: tcl-doc 1:9.0.2-1.fc43 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- fedora 44: tcl-doc 1:9.0.2-1.fc44 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- nixos 26.05: tcl 9.0.3
- nixpkgs unstable: tcl 9.1.0
- Copyright (c) 1990 The Regents of the University of California
- Copyright (c) 1994-1996 Sun Microsystems, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms