Memory problem with C-Extension

Marcus Stojek stojek at part-gmbh.de
Fri Mar 15 13:24:28 EST 2002


Hi,

Im my programm I am using a C-Extension (WinNT, Python 2.1.1,
Visual C++ V6)

I am passing a couple of arrays filled with data,some ints and one
array filled with 0s to the C-routine. The data is needed
for calculations (surprise) and the 0-array is a placeholder
for the expected int-results. (The values in my example are
only dummies of course).

Everything works fine, the results are correct but sooner or
later after I called the C-routine the program crashes with
a memory error. That's not reproducable and depends on
the size of the data I am working with. Sometimes it doesn't
happen at all.

I know that there is missing some information but maybe some
of you can see a BIG mistake (e.g. refrence counting)  that's
responsible. As I am neither
very good at Python, nor at C what you see is mostly copied, stolen
or guessed ;-) 

Thanks a lot in advance,
Marcus

Here is the condensed code :

Python-------------------------------------------------------------------------
        CmEleNr=10000
        CmEle=[(1,1,1) for  for e in range(CmEleNr)]
        CmEle=Numeric.array(CmEle) # array of int

        CmNodesNr=10000
        CmNodes=Numeric.array([(1.0,1.0,1.0) for n in
range(CmNodesNr)])# array of float

        AbaPtsNr=15000
        aip=[(1.0,1.0,1.0) for a in range(AbaPtsNr)]# array of float
        AbaPts=Numeric.array(aip)
#empty array of int that is filled by the C-routine:
        MasterEle=Numeric.array([0 for n in range(AbaPtsNr)])

        ip2cm = Numeric.array(MasterElement.MasterElement(MasterEle,

AbaPts,AbaPtsNr,CmEle,CmEleNr,CmNodes,CmNodesNr))

//C-----------------------------------------------------------------------
static PyObject* MasterElement (PyObject *self, PyObject *args)
{
  PyArrayObject *MasterEle, *AbaPts, *CmEle, *CmNodes;
  int AbaPtsNr, CmEleNr, CmNodesNr;

  if (!PyArg_ParseTuple(args, "O!O!iO!iO!i",
                              &PyArray_Type, &MasterEle,
                              &PyArray_Type, &AbaPts,  &AbaPtsNr,
                              &PyArray_Type, &CmEle,   &CmEleNr,
                              &PyArray_Type, &CmNodes, &CmNodesNr ) )
    return NULL;

//        printf ("%d  =  %d, %d\n", AbaPtsNr,
MasterEle->dimensions[0], MasterEle->strides[0]);

// The function that does all the work and fills MasterEle with ints
  MasterIntern ((int*)MasterEle->data, (double*)AbaPts->data,
AbaPtsNr,
                (int*)CmEle->data, CmEleNr, (double*)CmNodes->data,
CmNodesNr);

  return PyArray_Return (MasterEle);
}

static PyMethodDef MasterElement_methods[] ={
        {"MasterElement", MasterElement, METH_VARARGS},
        {0,0}
};

void initMasterElement (void)
{
        Py_InitModule("MasterElement", MasterElement_methods);
        import_array();
}



More information about the Python-list mailing list