ctypes problem: segfault with pointer usage

Bruno Cauet brunocauet at gmail.com
Fri Mar 27 08:03:53 EDT 2015


Hi,
I have a segfault problem with ctypes. Script attached.
I simply need to call `libgio.g_file_new_for_path` and then
`libgio.g_file_get_uri` with its result. That result is a GFile*. Since I
don't manipulate  that GFile* I don't think I have to mess with argtypes &
restype of the functions. I still tried ctypes.c_uint and ctypes.c_void_p.
In all cases after a few iterations g_file_get_uri will segfault. This does
not happen in the native version.
You will find python & c script attached.

Here is the result of a typical run:

$ LANG=C python2 ctypes_gio.py
Step 0
getting g_file_ptr for /foo/bar
getting uri_ptr with 8093280
free g_file_ptr
getting uri
free uri_ptr
Step 1
getting g_file_ptr for /foo/bar
getting uri_ptr with 8093344
free g_file_ptr
getting uri
free uri_ptr
Step 2
getting g_file_ptr for /foo/bar
getting uri_ptr with 8093824
free g_file_ptr
getting uri
free uri_ptr
Step 3
getting g_file_ptr for /foo/bar
getting uri_ptr with 8093440
free g_file_ptr
getting uri
free uri_ptr
Step 4
getting g_file_ptr for /foo/bar
getting uri_ptr with 8093472
free g_file_ptr
getting uri
free uri_ptr
Step 5
getting g_file_ptr for /foo/bar
getting uri_ptr with 8093856
free g_file_ptr
getting uri
free uri_ptr
Step 6
getting g_file_ptr for /foo/bar
getting uri_ptr with 140176928020160
free g_file_ptr
Erreur de segmentation (core dumped)


As you can see segfault happens as soon as the pointer position is outside
[0, 2^31[.
 This is why I tried messing with argument/response types.
What am I doing wrong? Any pointer (ahah) on how to solve my problem?

The c code can be compiled that way:
$ gcc $(pkg-config --cflags gio-2.0) segfault.c -o segfault $(pkg-config
--libs gio-2.0)

Thanks
Bruno
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20150327/840b29e7/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ctypes_gio.py
Type: text/x-python
Size: 1896 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20150327/840b29e7/attachment.py>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: segfault.c
Type: text/x-csrc
Size: 286 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20150327/840b29e7/attachment.c>


More information about the Python-list mailing list