[XML-SIG] Wierd bug in DOM cloneNode (or copy.deepcopy)

A.M. Kuchling akuchlin@cnri.reston.va.us
Sat, 31 Oct 1998 10:08:22 -0500

Sorry for the long time it took me to look into this; work was keeping
me busy with the preparations for an important demo.

Jeff.Johnson@stn.siemens.com writes:
 > file to reproduce the problem, I could get Guido to look into it.  So... I
 > took my DOM test file and put in an infinite loop to see if it would crash
 > on NT.  When I did, I noticed that __del__() is called more than __init__()
 > on xml.dom.core._nodeData when cloneNode() is used.  I don't know what kind
 > of wierd voodoo happens in the copy.deepcopy() function but it looks like
 > it messes up the reference counts somehow.

	Looking through the source code of copy.deepcopy(), it will
create the new _nodeData instance with the following sneaky code:

		y = _EmptyClass()
		y.__class__ = x.__class__

This will not call _nodeData's __init__ method, so the Node_counter
isn't incremented, which explains the apparent discrepancy in the
number of calls to __init__ and __del__; nothing is actually going
wrong.  Adding two __getinitargs__ methods to both classes fixes the
discrepancy, but I'm not sure if it's worth checking in the change,
since the Node_counter stuff is debugging code that will be taken out
before the final release.

 > That leads to a question about Python, how stable is the memory management?
 > Should I be surprised that it runs forever on Win98 but not on WinNT?  Does
 > copy.deepcopy do things it should not?

	It's stable, but the reference-counting garbage collection
used is vulnerable to getting fooled by cycles.  If you watch the
program size as the infinite loop runs, you'll see that it keeps
continuously growing.  One problem is that some of the test cases in
your code don't call p.close() as they should; I think test9() was one
of them.

	There are also some memory leaks in xmlproc that LMG will have
to look at when he gets some spare time.  Using the Expat module and
with p.close() being properly called, I can leave your test program
running indefinitely and the amount of memory it occupies remains

A.M. Kuchling			http://starship.skyport.net/crew/amk/
It is in the places where history was made that history is most sorely felt.
    -- Jeffrey Jacobs