Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #88150

Re: ctypes problem: segfault with pointer usage

References <CAMA3c4_7vpeeVTNdac3EP1ZW8POr5XtZMkt2Tr684aiP+Vfy0A@mail.gmail.com>
Date 2015-03-27 14:22 +0100
Subject Re: ctypes problem: segfault with pointer usage
From Bruno Cauet <brunocauet@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.248.1427462531.10327.python-list@python.org> (permalink)

Show all headers | View raw


[Multipart message — attachments visible in raw view] - view raw

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@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
>

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: ctypes problem: segfault with pointer usage Bruno Cauet <brunocauet@gmail.com> - 2015-03-27 14:22 +0100

csiph-web