473,385 Members | 2,044 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Return from C extension throws segmentation fault

2
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.surfarray.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:

Expand|Select|Wrap|Line Numbers
  1. static PyObject *
  2. TileTransform_getTransformedTile(TileTransform *self, PyObject *args)
  3. {
  4.     PyObject *source;
  5.  
  6.     //Parse the input parameters.
  7.     if (! PyArg_ParseTuple(args, "O", &source))
  8.         return NULL;
  9.  
  10.     return source;
  11. }
Is there some memory management I am failing to do?

Thanks for any help!
~Tale
Dec 21 '11 #1

✓ answered by Tale

Turns out that there is an issue with returning the original input from the same address that PyArg_ParseTuple 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...

Expand|Select|Wrap|Line Numbers
  1. static PyObject *
  2. TileTransform_getTransformedTile(TileTransform *self, PyObject *args)
  3. {
  4.         PyObject *source;
  5.     PyArrayObject *sourceArray;
  6.  
  7.     //Parse the input parameters.
  8.         if (! PyArg_ParseTuple(args, "OO", &source))
  9.         return NULL;
  10.  
  11.     //Translate the source to an array, (source, type, minimum dimensions, maximum dimensions).
  12.     sourceArray = (PyArrayObject *) PyArray_ContiguousFromObject(source, PyArray_LONG, 0, 3);
  13.  
  14.         //Checks and alterations to the array here.
  15.  
  16.     return PyArray_Return(resultArray);
  17. }
Hope this helps keep others from getting caught up on the same error.

~Tale

2 3361
Tale
2
Turns out that there is an issue with returning the original input from the same address that PyArg_ParseTuple 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...

Expand|Select|Wrap|Line Numbers
  1. static PyObject *
  2. TileTransform_getTransformedTile(TileTransform *self, PyObject *args)
  3. {
  4.         PyObject *source;
  5.     PyArrayObject *sourceArray;
  6.  
  7.     //Parse the input parameters.
  8.         if (! PyArg_ParseTuple(args, "OO", &source))
  9.         return NULL;
  10.  
  11.     //Translate the source to an array, (source, type, minimum dimensions, maximum dimensions).
  12.     sourceArray = (PyArrayObject *) PyArray_ContiguousFromObject(source, PyArray_LONG, 0, 3);
  13.  
  14.         //Checks and alterations to the array here.
  15.  
  16.     return PyArray_Return(resultArray);
  17. }
Hope this helps keep others from getting caught up on the same error.

~Tale
Dec 22 '11 #2
bvdet
2,851 Expert Mod 2GB
Tale - Thanks for the feedback! Hopefully your problem and solution will help others.
Dec 22 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: elho | last post by:
I called a own python type 'PyType' with a c function and returned it into my python programm - there it fault. It is said that the object has a NULL-Pointer when I try to debug it? Here are the...
2
by: Rolf Wester | last post by:
Hi, I' trying to make an extension module that passes Numeric arrays. The wrapper function is (swig generated and modified by myself): static PyObject *_wrap_my_func(PyObject *self, PyObject...
5
by: Fra-it | last post by:
Hi everybody, I'm trying to make the following code running properly, but I can't get rid of the "SEGMENTATION FAULT" error message when executing. Reading some messages posted earlier, I...
27
by: Paminu | last post by:
I have a wierd problem. In my main function I print "test" as the first thing. But if I run the call to node_alloc AFTER the printf call I get a segmentation fault and test is not printed! ...
8
by: lawrence k | last post by:
We made some changes to our server yesterday, and ever since, every single installation of WordPress that was on the server has stopped running. Other PHP scripts still run fine, but WordPress is...
7
by: pycraze | last post by:
I would like to ask a question. How do one handle the exception due to Segmentation fault due to Python ? Our bit operations and arithmetic manipulations are written in C and to some of our...
14
by: Vlad Dogaru | last post by:
Hello, I am trying to learn C, especially pointers. The following code attempts to count the appearences of each word in a text file, but fails invariably with Segmentation Fault. Please help me...
6
by: DanielJohnson | last post by:
int main() { printf("\n Hello World"); main; return 0; } This program terminate just after one loop while the second program goes on infinitely untill segmentation fault (core dumped) on...
27
by: Jess | last post by:
Hello, the following code failed with "segmentation fault". Could someone tell me why it failed please? Thanks! #include<iostream> #include<string> #include<cstring> #include<cstddef> using...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.