[Pyrex] Memory access problem

Michael Twomey mick at translucentcode.org
Sat Mar 6 23:04:11 CET 2004


Hi,

I seem to have run into a problem wrapping genx (a light weight XML
generation library). I've been making good progress up until my code
started core dumping (with an EXC_BAD_ACCESS memory access error on my
mac).

I've reduced it down to a test case:

cstuff.h, representing the library's header:

typedef struct cElement_rec * cElement;

cElement cDeclareElement(char * name);
int cStartElement(cElement element);

cstuff.c, representing the library:

#include "cstuff.h"
#include <stdio.h>

struct cElement_rec {
    char * name;
};

cElement cDeclareElement(char * name){
    cElement element;
    element->name = name;
    return element;
}

int cStartElement(cElement element) {
    printf("name: %s", element->name);
    return 0;
}

foo.pyx, my wrapper:

cdef extern from "cstuff.h":
    ctypedef struct cElement:
        char * name
    cElement * cDeclareElement(char * name)
    int cStartElement(cElement * element)

cdef class Element:
    cdef cElement * elem

cdef class Writer:
    def declareElement(self):
        cdef Element element
        cdef cElement * elem
        elem = cDeclareElement("element name")
        element = Element()
        element.elem = elem
        return element

    def startElement(self, element):
        cdef Element c_element
        cdef cElement * elem
        c_element = element
        elem = c_element.elem
        cStartElement(elem)

test.py, a simple test script:

import foo
class TestCase:
    def __init__(self):
        self.writer = foo.Writer()
        self.element = self.writer.declareElement()

    def test(self):
        self.writer.startElement(self.element)

tc = TestCase()
tc.test()

Running this produces a backtrace like this:

Program received signal EXC_BAD_ACCESS, Could not access memory.
cDeclareElement (name=0xc1004 "element name") at cstuff.c:10
10          element->name = name;
(gdb) bt
#0  cDeclareElement (name=0xc1004 "element name") at cstuff.c:10
#1  0x000c0844 in __pyx_f_3foo_6Writer_declareElement
(__pyx_v_self=0x533b8, __pyx_args=0xc07d0, __pyx_kwds=0x3) at foo.c:68
#2  0x95fa94a8 in PyEval_GetFuncDesc ()
#3  0x95fa6c64 in PyEval_EvalCode ()
#4  0x95fa7e30 in PyEval_EvalCodeEx ()
#5  0x95f5f354 in PyFunction_SetClosure ()
#6  0x95f4a8d0 in PyObject_Call ()
#7  0x95f529e8 in PyMethod_New ()
#8  0x95f4a8d0 in PyObject_Call ()
#9  0x95fa91ec in PyEval_CallObjectWithKeywords ()
#10 0x95f4de40 in PyInstance_New ()
#11 0x95f4a8d0 in PyObject_Call ()
#12 0x95fa9ba8 in PyEval_GetFuncDesc ()
#13 0x95fa9598 in PyEval_GetFuncDesc ()
#14 0x95fa6c64 in PyEval_EvalCode ()
#15 0x95fa7e30 in PyEval_EvalCodeEx ()
#16 0x95fa4734 in PyEval_EvalCode ()
#17 0x95fc85f0 in PyRun_FileExFlags ()
#18 0x95fc7668 in PyRun_SimpleFileExFlags ()
#19 0x95fd1ec0 in Py_Main ()
#20 0x00003c78 in start ()
#21 0x00003aec in start ()


I've attached a tarball of the test case (including a setup.py to build
it). I suspect I've done a real silly here, but I've tried numerous
permutations on the code (void *, playing with the ctypedefs and so on),
most of which either produced invalid C or the above error.

Any help would be appreciated.

(I'm using pyrex 0.9, Mac OS X, Apple's python and my debug build of 2.3.3)

thanks,
  mick
-------------- next part --------------
A non-text attachment was scrubbed...
Name: mem_problem.tar.gz
Type: application/x-gzip
Size: 912 bytes
Desc: not available
Url : http://lists.copyleft.no/pipermail/pyrex/attachments/20040306/72bcf40d/mem_problem.tar.bin


More information about the Pyrex mailing list