ctypes - How to do cleanup reliably in python? -


I have some CTPP binding, and for each body.No, I should call the body. The rest of the remaining allocation routines I am bound to the library is not isolated from the rest code (they can be said about anywhere), and to use two useful features, I need to make cyclical reference.

I think it will be solved if I find a reliable way of hooking the destructor for an object. (Weak RF) I want to give callback, before data has been deleted.

So obviously this code megafil when I put it in velocity:

body: body in body: def __init __ (self, mass, inertia) : Self. _body = body.New (mass, inertia) def __del __ (self): print '__del __% r'% Self Free (self ._a) ... def set_velocity_func (self, function): self _body.contents.velocity_func = ctypes_wrapping (func)

I also tried to solve it through the weakreference, with those things, which still looks worse, only large scale But more is unpredictable.

Even if I do not put in Velocity_func, then at least the circle will appear when I do:

  class toy (object): def __init __ (self, body): self.body.owner = self ... def collision (a, b, contact): whatever (a.body.owner)  

To ensure that the structures will collect garbage ed, even if they are allocated / freed from the shared library?

If you are interested in more details then: the repository is:

What you want to do, which creates an object that allocates things, and then the object is no longer in use, it is almost impossible in Python, unfortunately not guaranteed to be called the del statement That's why you can not trust him.

Python has the standard way:

  try: allocation () dosaf () end: cleanup ()  

Or since 2.5 you can also make reference managers and use them with a statement, which is a good way to do this.

But both of these mainly when you allocate / lock at the beginning of the code snippet. If you want the things allocated for the entire run of the program, then you need to allocate resources at startup before the main code of the program, and then dismiss it. There is a situation that is not covered here, and it happens when you want to allocate and delete many resources and use them in many places in the code. For example, you want memory buffers or similar pools. But most of these cases are for memory, which the dragon will handle for you, so you will not have to worry about those people. In such cases where you want to allocate the dynamic pool allocation of such things, which are not memory, and then you want the type of deallocation you are trying in your example, and that is is doing it with Python Difficult for


Comments

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -