> > The %anon_refs in Tcl.pm bugs me. It is used to remember sub- and > > variable-references that have been passed to Tcl. The drawback is > > that anything passed to Tcl then lives forever because it is kept > > alive by %anon_refs even if nothing else reference it. This is > > especially problematic for closures that are created fresh each time > > they are passed.
that said, it was designed to help with refcounting, but it appears it makes situation worse. Obviously, intention was to track objects so to not create an object within Tcl second time. > > > > I've played with patches that fixes this, but they do break the > > documented ;'{return,delete}_ref' API. Is this a problem? Anybody > > have code that uses it? IMHO it is safe to break that API. > > > > My current Tcl.pm patch collection is attached to this message. Thank you! I wish I could create patches of such a good quality! :) I think we should start using modified Tcl module... Will you apply those to CVS? > > > > > > Fixing scalar references > > ------------------------ > > > > When a scalar reference is passed Tcl.pm will create a shadow variable > > on the Tcl side and then tie the perl scalar in order to forward any > > Perl side fetch/store on it to the Tcl side. A reference to the > > scalar is kept in %anon_refs making sure the variable lives forever. > > > > Not adding the %anon_refs reference makes it possible for the variable > > to be garbage collected by perl when nothing else references it. By > > adding destructors to Tcl::Var we can then also clean up the shadow > > variable on the Tcl side. > > > > This is implemented by the patches: > > > > 189734-tclvar-dtor.patch > > 189813-var-unref.patch > > > > > > Fixing code references > > ---------------------- > > > > When a code reference is passed Tcl.pm will create a shadow command on > > the Tcl side that calls back to the Perl sub. The code reference is > > recorded in %anon_hash. This is needed to make sure the code is not > > garbage collected before the Tcl command get a chance to call back, > > but as a side effect the code will live forever even after there is no > > more references to it from the Tcl side. > > > > My proposed fix is to introduce a new Tcl object type that keeps a > > reference to the the real perl CV and that stringifies into something > > that is callable on the Tcl side. With this arrangement there is no > > need to record the code reference in %anon_hash to keep it alive, and > > when Tcl drops the reference to the function it is garbage collected > > properly on both the Tcl and Perl side. Very good. now as CV are of Tcl::Cb type, we can track their destruction by ordinary DESTROY method. Best regards, Vadim.