Python mem leaks?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tomas.bouda@systinet.com

    Python mem leaks?

    Hi,
    I'm facing a problem which seems to be a Python bug...

    I've got an application written in C and having Python embedded inside.
    The situation is the following:
    1) C object is wrapped by PyCObject and put into Python function
    2) The PyCObject has given a finalizer which frees the C object inside
    3) The Python function "do_smth(panel) " is called from C

    Having the two methods:

    def do_smth1(panel) :
    pass

    def do_smth2(panel) :
    raise Exception()

    Calling do_smth1() will cause returning of Py_None, the PyCObject <panel> is
    freed and the C finalizer (mentioned in no.2) is called.
    Calling do_smth2() will case returning NULL a <panel> is not freed nor
    finalizer is called. At this point I get a leak!

    Is it my fault? I didn't find anywhere in Python doc what should C app to do
    if such memory isn't freed. Seems more likely to be the Python leaking bug!

    Any comments or proposals?

    Thanks a lot, Tobbi



  • Duncan Booth

    #2
    Re: Python mem leaks?

    tomas.bouda@sys tinet.com wrote in
    news:mailman.69 .1070454775.168 79.python-list@python.org :
    [color=blue]
    > Having the two methods:
    >
    > def do_smth1(panel) :
    > pass
    >
    > def do_smth2(panel) :
    > raise Exception()
    >
    > Calling do_smth1() will cause returning of Py_None, the PyCObject
    > <panel> is freed and the C finalizer (mentioned in no.2) is called.
    > Calling do_smth2() will case returning NULL a <panel> is not freed nor
    > finalizer is called. At this point I get a leak!
    >
    > Is it my fault? I didn't find anywhere in Python doc what should C app
    > to do if such memory isn't freed. Seems more likely to be the Python
    > leaking bug![/color]

    It is not your fault, but neither is it a memory leak or a bug in Python.
    When your function throws an exception the stack frame is held in case
    whatever handles the exception wants to inspect it. This extends the
    lifetime of all the objects references within the function until the stack
    frame is discarded which is not usually until another exception is thrown.

    --
    Duncan Booth duncan@rcp.co.u k
    int month(char *p){return(1248 64/((p[0]+p[1]-p[2]&0x1f)+1)%12 )["\5\x8\3"
    "\6\7\xb\1\x9\x a\2\0\4"];} // Who said my code was obscure?

    Comment

    • tomas.bouda@systinet.com

      #3
      Re: Python mem leaks?

      Hi,
      you're right, my objects were held in Python backtrace.
      That was what I thought but I was a bit confused 'cause PyErr_Clear() didn't
      clear the backtrace, just set the exception flag.

      Now, I'm calling { PyErr_SetNone(P yExc_Exception) ; PyErr_Clear(); } which
      clears memory. Probably a hack but works fine.

      Thanks a lot for your help.

      Best Regards,
      Tobbi


      A.T.Hofkamp writes:
      [color=blue]
      > In comp.lang.pytho n, you wrote:[color=green]
      >>
      >> def do_smth2(panel) :
      >> raise Exception()
      >>
      >> Calling do_smth1() will cause returning of Py_None, the PyCObject <panel> is
      >> freed and the C finalizer (mentioned in no.2) is called.
      >> Calling do_smth2() will case returning NULL a <panel> is not freed nor
      >> finalizer is called. At this point I get a leak![/color]
      >
      > What happens if you handle the exception?
      >
      > My guess is that in the stack backtrace, there is a reference to panel.
      > Since handling the exception may mean printing the stack, Python still
      > needs the object.
      > If you handle the exception, I would expect Python to release the stack
      > trace information, and thus the object.
      >
      >
      > Albert
      > --
      > Unlike popular belief, the .doc format is not an open publically available format.
      >[/color]



      Comment

      Working...