Submitted by : simon at: 2003-10-26T21:32:10+00:00 (14 years ago)
Name :
Category : Severity : Status :
Optional subject :  
Optional comment :


comments:

Re: ZWiki memory leaks --Simon Michael, 2003/06/22 09:18 GMT reply
Shane Hathaway wrote:
> I've been tracking down memory leaks on dev.nzo.zope.org and I found
> several in the ZWiki product. They all take the form:

 try:
       do_something()
 except:
       type,val,tb = sys.exc_info()
       err = join(traceback.format_exception(type,val,tb),'')
       return "&lt;html><body>...<pre>%s</pre>\n</body></html>" % err


> The problem is the "tb" variable. It carries a reference to the entire
> stack, including the local variables, and the local variables include
> "tb", hence creating a circular reference. This is a Python "gotcha". The
> way to deal with this is to get rid of references to tracebacks as soon as
> possible. Since you're repeating some code anyway, it would make sense to
> create a function that formats tracebacks for you. For example:

 def formatWikiException(message):
      type,val,tb = sys.exc_info()
      try:
          err = join(traceback.format_exception(type,val,tb),'')
          return ("<html><body>%s<pre>%s</pre>\n</body></html>" %
                  (message, err))
      finally:
          del tb  # Clean up circular reference


> Note the try/finally that cleans up "tb" even if the code fails for any
> reason.

(Thank you!)

fixed for 0.20 --SimonMichael, 2003/06/22 09:50 GMT reply
Status: open => closed