[Pyrex] Pyrex 0.9: Compiled code produces segmentation fault

Paul Prescod paul at prescod.net
Wed Jan 28 02:08:39 CET 2004


Matthias Baas wrote:
> Hi,
> 
> I've come across a weird bug that can lead to a crash when Pyrex 
> generated code is executed.
> I have an extension type "vec3" that represents vectors with 3 float 
> components and an accompanying iterator class "vec3iter" to iterate over 
> the components. But using the iterator can lead to a segmentation fault. 
>  However, it doesn't always happen, sometimes the iterator works as 
> expected but suddenly it crashes.
> I've seen it crash on both, Windows (XP, MSVC6) and Linux. And it only 
> occurs when using Python 2.2.2. All tests have been ok so far when using 
> Python 2.3.
> The problem also does not occur when using Pyrex 0.8.2 instead of 0.9.
> 
> Now here's an example showing the problem:

It seems to me that by the time Python calls the iterator object's 
tp_dealloc function, the object has already been freed. I don't know why 
that is. But I can observe it happening because on my platform the 
memory manager is complaining about a double free of an object when the 
tp_dealloc tries to free the object. I'm not setup to debug into Python 
so that's all I can figure out right now. You can see the effect in 
action with this instrumented dealloc function:

static void __pyx_tp_dealloc_3vec_vec3iter(PyObject *o){
   struct __pyx_obj_3vec_vec3iter *p = (struct __pyx_obj_3vec_vec3iter *)o;
   printf("Deallocating iterator @ %p with refcnt %d\n", p, p->ob_refcnt);
   Py_XDECREF(p->v);
   (*o->ob_type->tp_free)(o);
   printf("freed %p\n", o);
}

I get:

Deallocating iterator @ 0x2aeb10 with refcnt 0
*** malloc[1944]: Deallocation of a pointer not malloced: 0x2aeb10; This 
could be a double free(), or free() called with the middle of an 
allocated block; Try setting environment variable MallocHelp to see 
tools to help debug
freed 0x2aeb10

I don't know why the iterator would have already been freed when its 
deallocator is called. It should be the job of the deallocator to do the 
freeing.

  Paul Prescod





More information about the Pyrex mailing list