Tcl/Tk Documentation > TclLib > TCL_MEM_DEBUGTcl_InitMemory to add the memory command to Tcl.
TCL_MEM_DEBUG must be either left defined for all modules or undefined for all modules that are going to be linked together. If they are not, link errors will occur, with either Tcl_DbCkfree and Tcl_DbCkalloc or Tcl_Alloc and Tcl_Free being undefined.ckalloc is made, slightly more memory than requested is allocated so the memory debugging code can keep track of the allocated memory, and eight-byte “guard zones” are placed in front of and behind the space that will be returned to the caller. (The sizes of the guard zones are defined by the C #define LOW_GUARD_SIZE and #define HIGH_GUARD_SIZE in the file generic/tclCkalloc.c — it can be extended if you suspect large overwrite problems, at some cost in performance.) A known pattern is written into the guard zones and, on a call to ckfree, the guard zones of the space being freed are checked to see if either zone has been modified in any way. If one has been, the guard bytes and their new contents are identified, and a “low guard failed” or “high guard failed” message is issued. The “guard failed” message includes the address of the memory packet and the file name and line number of the code that called ckfree. This allows you to detect the common sorts of one-off problems, where not enough space was allocated to contain the data written, for example. ckalloc, again, at a large performance impact.
If you are desperate and validating memory on every call to ckalloc and ckfree is not enough, you can explicitly call Tcl_ValidateAllMemory directly at any point. It takes a char * and an int which are normally the filename and line number of the caller, but they can actually be anything you want. Remember to remove the calls after you find the problem.ckalloc, memory, Tcl_ValidateAllMemory, Tcl_DumpActiveMemory memory, debug