ctypes problem: segfault with pointer usage

Bruno Cauet brunocauet at gmail.com
Fri Mar 27 09:22:09 EDT 2015


Found the problem: I just have to add libgio.g_object_unref.argtypes =
[ctypes.c_void_p]
It now works flawlessly! Sorry for the noise.

2015-03-27 13:03 GMT+01:00 Bruno Cauet <brunocauet at gmail.com>:

> 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/b5aa852f/attachment.html>


More information about the Python-list mailing list