Re: Py_XDECREF() causes segmentation fault in _call_function_pointer()
Thomas Heller <[email protected]>
| Newsgroups | gmane.comp.python.ctypes |
|---|---|
| Message-ID | <[email protected]> |
John Dama (jdama) schrieb:
> Hi. We are consistently getting a segmentation violation in _ctypes.so
> in _call_function_pointer() for a specific function.
>
> The problem disappears if I comment out statement
> "Py_XDECREF(error_object)" in callproc.c _call_function_pointer().
>
> error_object is NULL when seg fault occurs so Py_XDECREF(error_object)
> should effectively be doing nothing yet causes a seg fault.
Ok, so it seems that the function call has damaged something (the stack,
local vars, something like that).
Which also means that commenting out the Py_XDECREF() call isn't a solution.
> The problem function looks like:
>
> int myfunc( tHandle handle, tNames name, void* pValue ) where "typedef
> void* tHandle" and "typedef enum { ... } tNames"
What does the function do? Will it return some information
(means: write into the memory location) pointed to by the third argument?
>
>
> so the corresponding python/ctypes call looks like
>
> mylib = ctypes.CDLL('/lib/mylib.so')
>
> c_value = ctypes.c_uint16( 0x00 )
>
> mylib.myfunc( ctypes.c_void_p( 1 ), c_int( 4 ), ctypes.cast(
> ctypes.byref( c_value ), ctypes.c_void_p ) )
If the function will write something into the third argument then
you are calling it incorrectly. The third argument may not be what
you mean.
Assuming you have to pass the address of an integer variable as third arg,
then you should use code like the following:
c_value = ctypes.c_uint16(0x00)
mylib.myfunc(ctypes.c_void_p(1), c_int(4), ctypes.byref(c_value))
Anyway, it is always safer to define argtypes (and possible restype) for
functions, especially on machines where sizeof(int) != sizeof(void *).
In your case, probably:
mylib.myfunc.argtypes = [ctypes.c_void_p, ctypes.c_int, ctypes.c_void_p]
# After that, the following function call does the same as the above example,
# but will automatically convert and check the arguments:
mylib.myfunc(1, 4, byref(c_value))
--
Thanks,
Thomas
------------------------------------------------------------------------------
Download Intel® Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev