[XML-SIG] Is this a memory leak?

Mark Bucciarelli mark@easymailings.com
Tue, 6 May 2003 20:13:09 -0400


#!/usr/bin/python

import xml.sax
import StringIO
import gc

gc.set_debug(gc.DEBUG_SAVEALL)

class TestHandler(xml.sax.handler.ContentHandler):
    all_things = {}

    def __init__(self):
        TestHandler.all_things[id(self)] = 1
    def __del__(self):
        del TestHandler.all_things[id(self)]

for i in range(400):
    parser = xml.sax.make_parser()
    t = TestHandler()
    parser.setContentHandler(t)
    input = xml.sax.xmlreader.InputSource()
    input .setByteStream(StringIO.StringIO('bad soap request'))
    try: parser.parse(input)
    except: pass

gc.collect()
print 'garbage', len(gc.garbage)   # 11970
print len(TestHandler.all_things)  # 400

Moving the parser = xml.sax.make_parser() out of the loop changes the 
print statement output 0 and 1.

Mark

P.S.  Occurs on both:
python 2.2.1, Linux, has .../site-packages/_xmlplus
python 2.2.2, XP, doesn't have .../site-packages/_xmlplus

P.P.S While working on this problem, I found a great blog entry by 
Jeremy Hylton on debugging memory leaks at:

http://www.python.org/~jeremy/weblog/030410.html

Looks like the next release of ZODB (and Zope) will have some leaks 
plugged.