In message <[EMAIL PROTECTED]> Marc Munro <[EMAIL PROTECTED]> wrote:
> On Thu, 2007-23-08 at 12:14 -0600, Charlie Savage wrote: > > > If not, can you install a callback into libxml to be alerted when a node > > is freed? If so, then you could decrement the reference count. But > > you'd still have to keep some long-term memory around so that any > > existing Ruby objects pointing to the node could check the reference > > count and see if it is zero. How do you know when to free that memory? > > I see no way, unless you go to the Ruby object <-> libxml object mapping > > like I described in the last email. > > I don't know if libxml2 provides such callbacks, but I don't see how > this would work anyway. The xmlNode has no idea what ruby objects are > referencing it. Instead of trying to ensure that we free the ruby > object when the xmlNode is freed, we have to ensure that such nodes > referenced by ruby objects are not freed at all by xmlFree. The way I did it is to have the ruby object point at a structure belonging to the wrapper which in turn points to the real libxml object. That intermediate structure also has a reference count which records how many ruby objects (but not libxml objects) point at it. In addition the _private member of the libxml object is set to point back at the intermediate structure so that when libxml calls the callback you can find that additional information and see if any ruby objects still reference it. If a ruby object does still reference it then the libxml object is cloned (as we can't stop libxml deleting the one it has called us back about) and the intermediate structure is changed to point at that clone instead of the original copy that is about the be deleted. Tom -- Tom Hughes ([EMAIL PROTECTED]) http://www.compton.nu/ _______________________________________________ libxml-devel mailing list libxml-devel@rubyforge.org http://rubyforge.org/mailman/listinfo/libxml-devel