I have been using cython for some time now without any problem. I make extensive use of malloc functions in most modules of my project, yet in this particular module realloc fails me in a strange way. Here is the faulty function:
cdef MeshFace* addFace(self, cVector* p1, cVector* p2, cVector* p3, cVector* normal): cdef cVector* pts[3] cdef MeshFace* f = NULL cdef void* ptr = NULL pts[0] = p1 pts[1] = p2 pts[2] = p3 if(self._facenum >= self._facemem - 2): self._facemem = <int>(<double>self._facemem*1.25) ptr = realloc(self._faceList, self._facemem*sizeof(MeshFace)) if ptr == NULL: return NULL self._faceList = ptr f = &self._faceList[self._facenum] MFace_init2(f, &pts[0], 3, NULL) self._facenum += 1
This function gets called multiple times to add faces to the mesh. Yet when the "facenum" values reaches somewhere around 600, python raises a memory error: error for object 0x100bef800: incorrect checksum for freed object - object was probably modified after being freed.
Other places I do use malloc without ANY problem. BTW: I run the program on a MacBook Pro (8GB RAM)
What am I doing wrong?
NB: the variable "_faceList" is initialized further up in the code using a malloc on 512 unit of struct MeshFace
self._faceList = ptr
- DavidW_faceList
is. You could allocate something, free something, not clear the pointer and then when_faceList
is realloced it could end up with the same address as the pointer you were using before - DavidW