next up previous contents index
Next: 18.5.2 C/C++ Up: 18.5 Memory Leaks Previous: 18.5 Memory Leaks

   
18.5.1 OTcl

OTcl, especially TclCL, provides a way to allocate new objects. However, it does not accordingly provide a garbage collection mechanism for these allocated objects. This can easily lead to unintentional memory leaks. Important: tools such as dmalloc and purify are unable to detect this kind of memory leaks. For example, consider this simple piece of OTcl script:
set ns [new Simulator]
for {set i 0} {$i \< 500} {incr i} {
        set a [new RandomVariable/Constant]
}
One would expect that the memory usage should stay the same after the first RandomVariable is allocated. However, because OTcl does not have garbage collection, when the second RandomVariable is allocated, the previous one is not freed and hence results in memory leak. Unfortunately, there is no easy fix for this, because garbage collection of allocated objects is essentially incompatible with the spirit of Tcl. The only way to fix this now is to always explicitly free every allocated OTcl object in your script, in the same way that you take care of malloc-ed object in C/C++.




2000-08-24