> > Lisandro Dalcin (author of mpi4py) came up with the following trick > > that, while more complicated, prevents memory leaks: > > > > cdef extern from "Python.h": > > object PyString_FromStringAndSize(char*,Py_ssize_t) > > char* PyString_AS_STRING(object) > > > > cdef inline object pyalloc_i(int size, int **i): > > if size < 0: size = 0 > > cdef Py_ssize_t n = size * sizeof(int) > > cdef object ob = PyString_FromStringAndSize(NULL, n) > > i[0] = <int*> PyString_AS_STRING(ob) > > return ob > > > > and now > > > > def foo(sequence): > > cdef int size = len(sequence), > > cdef int *buf = NULL > > cdef object tmp = pyalloc_i(size, &buf) > > > > This could probably be adapted into a malloc-like function. What do > > people think? > > Could you explain what the point is? Is it that this is a trick so that > Cython will correctly garbage collect the allocated memory, even > if an exception occurs?
Yes, that is the idea. By having a python object that knows about the memory, the garbage collection should prevent memory leaks if an exception occurs. I am not sure if Lisandro has proved that this is the case - he is just wondering how people typically handle this case. Seems like we are not alone in thinking this is a problem though. Brian > -- William > > > > --~--~---------~--~----~------------~-------~--~----~ To post to this group, send email to sage-devel@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sage-devel URLs: http://www.sagemath.org -~----------~----~----~----~------~----~------~--~---