On Thu, 2007-23-08 at 13:04 -0600, Charlie Savage wrote: > > > 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. > > Boy - that's tough in a tree. What if you have a reference to a leaf > node, but no longer to the root. How are you supposed to free the whole > tree when you are done with the leaf?
I'm not sure why this seems tough. If we have a reference to a leaf when we want to garbage collect the rest of the tree, I am suggesting removing that leaf from the tree prior to freeing it. > > I don't think it requires any more mapping than is in place now, with > > each ruby object knowing the xmlNode it represents. Beyond that the > > reference counting should be enough. > > Not if libxml frees the memory from under you. So your point of view is > that Ruby entirely controls the memory allocation/deallocation. But see > my point above for why that could be tough. Ruby controls which libxml trees are freed, but has no say about how. If I understand things correctly, calling xmlFree on a node, frees that node and all of its descendants, so removing any referenced node from the tree before freeing it should be enough. > > Right now, each xmlNode knows how many ruby objects it is referenced > > from (assuming the reference counting works). When a ruby object is > > freed, it decrements the reference count and if it is the last object > > referencing the xmlNode, and that xmlNode is not referenced by parent > > nodes, then the xmlNode is freed using xmlFree. The problem is that > > xmlFree will free child nodes without checking whether they are > > referenced from ruby. That's why I think we need to walk the tree and > > remove such nodes prior to letting xmlFree do its stuff. > > Walk what tree? The Ruby tree (thereby creating a bunch of new ruby > objects and unneeded references)? The libxml c tree? If so, you are > back to having to know which Ruby objects point to which libxml object. I meant walk the libxml c tree. I don't think we need to care *which* ruby objects reference an xmlNode, just whether *any* ruby object does. If any ruby object references a node, that node must not be be freed by xmlFree. This, of course, assumes that the ruby libxml reference counting approach works which may not be true. > > I don't see how this would be possible. My target documents are built > > one node at a time from ruby objects. I think ruby needs to own those > > objects. > > When you add the Ruby objects to a tree, the Ruby objects can give up > ownership (that's simple, set the free method to nil). That would be quite a change to the API. As it is I can do either of these things and both seem reasonable: doc = XML::Document.new root = XML::node.new('node') doc.root = root root['wibble'] = 'wibble' or doc = XML::Document.new root = XML::node.new('node') root['wibble'] = 'wibble' doc.root = root I suspect my app does both, so I wouldn't like to see that change. __ Marc
signature.asc
Description: This is a digitally signed message part
_______________________________________________ libxml-devel mailing list libxml-devel@rubyforge.org http://rubyforge.org/mailman/listinfo/libxml-devel