Return from C extension throws segmentation fault

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Tale
    New Member
    • Dec 2011
    • 2

    Return from C extension throws segmentation fault

    Hi all,

    I have been making a C extension to Python recently. This extension has a function that takes a 3D array (as made by pygame.surfarra y.pixels3d), is going to do operations on this array, and then return it.

    My problem is, that the most basic form of this function is throwing a segmentation fault after 58 calls from my program. The input is always a 3D array of integers, 1st dimension of size 70, second of size 36, third of size 3. I have ran a test where each input is identical in every way, but the function still faults.

    Here is the code:

    Code:
    static PyObject *
    TileTransform_getTransformedTile(TileTransform *self, PyObject *args)
    {
    	PyObject *source;
    
    	//Parse the input parameters.
        if (! PyArg_ParseTuple(args, "O", &source))
            return NULL;
    
    	return source;
    }
    Is there some memory management I am failing to do?

    Thanks for any help!
    ~Tale
  • Tale
    New Member
    • Dec 2011
    • 2

    #2
    Turns out that there is an issue with returning the original input from the same address that PyArg_ParseTupl e stores the parameter in.

    If I convert the PyObject to a PyObjectArray (as below) and always return that array instead of the original input (earlier, my code was returning the original input when no changes were necessary), then the function does not throw the segmentation fault that I was getting earlier.

    The code that works is...

    Code:
    static PyObject *
    TileTransform_getTransformedTile(TileTransform *self, PyObject *args)
    {
            PyObject *source;
    	PyArrayObject *sourceArray;
    
    	//Parse the input parameters.
            if (! PyArg_ParseTuple(args, "OO", &source))
            return NULL;
    
    	//Translate the source to an array, (source, type, minimum dimensions, maximum dimensions).
    	sourceArray = (PyArrayObject *) PyArray_ContiguousFromObject(source, PyArray_LONG, 0, 3);
    
            //Checks and alterations to the array here.
    
    	return PyArray_Return(resultArray);
    }
    Hope this helps keep others from getting caught up on the same error.

    ~Tale

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      Tale - Thanks for the feedback! Hopefully your problem and solution will help others.

      Comment

      Working...