Python mem leaks?

Discussion in 'Python' started by tomas.bouda@systinet.com, Dec 3, 2003.

  1. Guest

    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
     
    , Dec 3, 2003
    #1
    1. Advertising

  2. Duncan Booth Guest

    wrote in
    news::

    > 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!


    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
    int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
    "\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?
     
    Duncan Booth, Dec 3, 2003
    #2
    1. Advertising

  3. Guest

    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(PyExc_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:

    > In comp.lang.python, you wrote:
    >>
    >> 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!

    >
    > 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.
    >
     
    , Dec 3, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rob
    Replies:
    0
    Views:
    383
  2. Anders K. Jacobsen [DK]

    ASP.NET MEM profiler

    Anders K. Jacobsen [DK], Jun 7, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    497
    Anders K. Jacobsen [DK]
    Jun 7, 2005
  3. py_genetic

    pytables - best practices / mem leaks

    py_genetic, Jul 17, 2006, in forum: Python
    Replies:
    1
    Views:
    873
    py_genetic
    Jul 18, 2006
  4. Replies:
    8
    Views:
    334
    jaysome
    Mar 1, 2007
  5. news.aon.at
    Replies:
    11
    Views:
    668
    Ian Collins
    Jan 29, 2011
Loading...

Share This Page