How to do cleanup reliably in python?

Posted by Cheery on Stack Overflow See other posts from Stack Overflow or by Cheery
Published on 2009-06-25T13:53:08Z Indexed on 2010/05/19 6:30 UTC
Read the original article Hit count: 311

Filed under:
|
|

I have some ctypes bindings, and for each body.New I should call body.Free. The library I'm binding doesn't have allocation routines insulated out from the rest of the code (they can be called about anywhere there), and to use couple of useful features I need to make cyclic references.

I think It'd solve if I'd find a reliable way to hook destructor to an object. (weakrefs would help if they'd give me the callback just before the data is dropped.

So obviously this code megafails when I put in velocity_func:

class Body(object):
    def __init__(self, mass, inertia):
        self._body = body.New(mass, inertia)

    def __del__(self):
        print '__del__ %r' % self
        if body:
            body.Free(self._body)

    ...        

    def set_velocity_func(self, func):
        self._body.contents.velocity_func = ctypes_wrapping(func)

I also tried to solve it through weakrefs, with those the things seem getting just worse, just only largely more unpredictable.

Even if I don't put in the velocity_func, there will appear cycles at least then when I do this:

class Toy(object):
    def __init__(self, body):
        self.body.owner = self

...

def collision(a, b, contacts):
    whatever(a.body.owner)

So how to make sure Structures will get garbage collected, even if they are allocated/freed by the shared library?

There's repository if you are interested about more details: http://bitbucket.org/cheery/ctypes-chipmunk/

© Stack Overflow or respective owner

Related posts about python

Related posts about cyclic-reference