unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* segmenation fault
@ 2017-06-09  7:21 Catonano
  2017-06-09  7:43 ` Thomas Danckaert
  0 siblings, 1 reply; 10+ messages in thread
From: Catonano @ 2017-06-09  7:21 UTC (permalink / raw)
  To: guile-user

in using the guile FFI I am having a segmentation fault

This is the C unction that I'm trying to wrap

https://www.gaia-gis.it/gaia-sins/freexl-1.0.1-doxy-doc/html/freexl_8h.html#acbd27ba5bc7b21d4ae32c0542d51f1e4

My code is here
https://gitlab.com/humanitiesNerd/guile-freexl
(configure is to be called with --with-freexl-libdir=... otherwise it won't
work)

Here's an excerpt

(define freexl-open
  (let* ((ptr     (freexl-func "freexl_open"))
         (proc    (pointer->procedure int ptr '(* *)))
     ;;const char *path, const void **xls_handle
     )
  (lambda (path)
    (let ((handle (scm->pointer 0))
      (path-ptr (string->pointer path)))
      (proc path-ptr handle)))))


At the REPL, when calling it with
> (freexl-open"resources/Lavoro_P.xsl")

it produces a segmentation fault

probably this line
(scm->pointer 0)
is not correct

But I don't know how to produce a void pointer and then pass it to
freexl-open

Thanks in advance


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: segmenation fault
  2017-06-09  7:21 segmenation fault Catonano
@ 2017-06-09  7:43 ` Thomas Danckaert
  2017-06-09  8:00   ` Amirouche Boubekki
  2017-06-09  8:42   ` Catonano
  0 siblings, 2 replies; 10+ messages in thread
From: Thomas Danckaert @ 2017-06-09  7:43 UTC (permalink / raw)
  To: catonano; +Cc: guile-user

From: Catonano <catonano@gmail.com>
Subject: segmenation fault
Date: Fri, 9 Jun 2017 09:21:35 +0200

> probably this line
> (scm->pointer 0)
> is not correct
>
> But I don't know how to produce a void pointer and then pass it to
> freexl-open

It seems you need a void ** (pointer to void pointer).  I'm not 
familiar with Guile FFI, but (scm->pointer 0) is probably a pointer 
to an int?

Perhaps (scm->pointer %null-pointer) will return a void**?

Thomas



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: segmenation fault
  2017-06-09  7:43 ` Thomas Danckaert
@ 2017-06-09  8:00   ` Amirouche Boubekki
  2017-06-09  8:17     ` Catonano
  2017-06-11 20:56     ` Catonano
  2017-06-09  8:42   ` Catonano
  1 sibling, 2 replies; 10+ messages in thread
From: Amirouche Boubekki @ 2017-06-09  8:00 UTC (permalink / raw)
  To: Thomas Danckaert; +Cc: guile-user

 It's something like (bytevector->pointer (make-vector)) there is a
make-double-pointer procedure in guile git

Le 9 juin 2017 9:47 AM, "Thomas Danckaert" <post@thomasdanckaert.be> a
écrit :

> From: Catonano <catonano@gmail.com>
> Subject: segmenation fault
> Date: Fri, 9 Jun 2017 09:21:35 +0200
>
> probably this line
>> (scm->pointer 0)
>> is not correct
>>
>> But I don't know how to produce a void pointer and then pass it to
>> freexl-open
>>
>
> It seems you need a void ** (pointer to void pointer).  I'm not familiar
> with Guile FFI, but (scm->pointer 0) is probably a pointer to an int?
>
> Perhaps (scm->pointer %null-pointer) will return a void**?
>
> Thomas
>
>


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: segmenation fault
  2017-06-09  8:00   ` Amirouche Boubekki
@ 2017-06-09  8:17     ` Catonano
  2017-06-09 11:18       ` Chris Vine
  2017-06-11 20:56     ` Catonano
  1 sibling, 1 reply; 10+ messages in thread
From: Catonano @ 2017-06-09  8:17 UTC (permalink / raw)
  To: Amirouche Boubekki; +Cc: guile-user

Amirouche,

2017-06-09 10:00 GMT+02:00 Amirouche Boubekki <amirouche.boubekki@gmail.com>
:

>  It's something like (bytevector->pointer (make-vector)) there is a
> make-double-pointer procedure in guile git
>
>

Thank you !!

I took a look at the guile-gcrypt code and I found

(bytevector->pointer (make-bytevector (sizeof '*))

I was going to write this right now !

It works !
I just called freexl-open and it returned 0 !!

Thanks so much !


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: segmenation fault
  2017-06-09  7:43 ` Thomas Danckaert
  2017-06-09  8:00   ` Amirouche Boubekki
@ 2017-06-09  8:42   ` Catonano
  1 sibling, 0 replies; 10+ messages in thread
From: Catonano @ 2017-06-09  8:42 UTC (permalink / raw)
  To: Thomas Danckaert; +Cc: guile-user

Thomas,

2017-06-09 9:43 GMT+02:00 Thomas Danckaert <post@thomasdanckaert.be>:

> From: Catonano <catonano@gmail.com>
> Subject: segmenation fault
> Date: Fri, 9 Jun 2017 09:21:35 +0200
>
> probably this line
>> (scm->pointer 0)
>> is not correct
>>
>> But I don't know how to produce a void pointer and then pass it to
>> freexl-open
>>
>
> It seems you need a void ** (pointer to void pointer).  I'm not familiar
> with Guile FFI, but (scm->pointer 0) is probably a pointer to an int?
>
> Perhaps (scm->pointer %null-pointer) will return a void**?
>


I had overlooked your reply !

Thanks to you too !!


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: segmenation fault
  2017-06-09  8:17     ` Catonano
@ 2017-06-09 11:18       ` Chris Vine
  2017-06-09 16:36         ` Josh Datko
  2017-06-09 20:18         ` Catonano
  0 siblings, 2 replies; 10+ messages in thread
From: Chris Vine @ 2017-06-09 11:18 UTC (permalink / raw)
  To: guile-user

On Fri, 9 Jun 2017 10:17:18 +0200
Catonano <catonano@gmail.com> wrote:
> 2017-06-09 10:00 GMT+02:00 Amirouche Boubekki
> <amirouche.boubekki@gmail.com> :
> 
> >  It's something like (bytevector->pointer (make-vector)) there is a
> > make-double-pointer procedure in guile git
> 
> Thank you !!
> 
> I took a look at the guile-gcrypt code and I found
> 
> (bytevector->pointer (make-bytevector (sizeof '*))
> 
> I was going to write this right now !
> 
> It works !
> I just called freexl-open and it returned 0 !!

If you want to manipulate a scheme bytevector at the C level you can
also look at the c-write procedure here for ideas:
https://github.com/ChrisVine/guile-a-sync/blob/master/lib/unix_write.c

(As a point of detail, this procedure is only required with guile-2.0 -
with guile-2.2, suspendable ports are used instead - but it shows you
one approach to working with scheme buffers or other objects.)

Chris



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: segmenation fault
  2017-06-09 11:18       ` Chris Vine
@ 2017-06-09 16:36         ` Josh Datko
  2017-06-09 20:18         ` Catonano
  1 sibling, 0 replies; 10+ messages in thread
From: Josh Datko @ 2017-06-09 16:36 UTC (permalink / raw)
  To: guile-user

> On Fri, 9 Jun 2017 10:17:18 +0200
> Catonano <catonano@gmail.com> wrote:
> > 
> If you want to manipulate a scheme bytevector at the C level you can
> also look at the c-write procedure here for ideas:
> https://github.com/ChrisVine/guile-a-sync/blob/master/lib/unix_write.
> c
> 
> (As a point of detail, this procedure is only required with guile-2.0 
> -
> with guile-2.2, suspendable ports are used instead - but it shows you
> one approach to working with scheme buffers or other objects.)

When I scheme in C (lol, sounds funny), I ended up with bytevector code
like this:

size_t len = scm_c_bytevector_length (bv);
signed char * SCM_BYTEVECTOR_CONTENTS (bv);

Now, I'm pretty new at this, so the reason I'm asking it to seek
opinions on a best practice re: this thread.

The reason I write it that way is that, I think, the
scm_c_bytevector_length will throw an exception if it's not a
bytevector. This lets me be a bit lazy and not explicitly check. Then I
get the pointer to the bv.

This could just be a case of there-are-many-ways-to-do-it, but I would
have not thought to do:

void* c_buf = scm_to_pointer(scm_bytevector_to_pointer(bv, begin));

per that example so I was curious if there was an advantage or
something wrong with my thinking of how I Scheme in C :)

I'm using Guile 2.0.11 on Ubuntu.

Josh



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: segmenation fault
  2017-06-09 11:18       ` Chris Vine
  2017-06-09 16:36         ` Josh Datko
@ 2017-06-09 20:18         ` Catonano
  1 sibling, 0 replies; 10+ messages in thread
From: Catonano @ 2017-06-09 20:18 UTC (permalink / raw)
  To: Chris Vine; +Cc: guile-user

Chris,

2017-06-09 13:18 GMT+02:00 Chris Vine <vine35792468@gmail.com>:

> On Fri, 9 Jun 2017 10:17:18 +0200
> Catonano <catonano@gmail.com> wrote:
> > 2017-06-09 10:00 GMT+02:00 Amirouche Boubekki
> > <amirouche.boubekki@gmail.com> :
> >
> > >  It's something like (bytevector->pointer (make-vector)) there is a
> > > make-double-pointer procedure in guile git
> >
> > Thank you !!
> >
> > I took a look at the guile-gcrypt code and I found
> >
> > (bytevector->pointer (make-bytevector (sizeof '*))
> >
> > I was going to write this right now !
> >
> > It works !
> > I just called freexl-open and it returned 0 !!
>
> If you want to manipulate a scheme bytevector at the C level you can
> also look at the c-write procedure here for ideas:
> https://github.com/ChrisVine/guile-a-sync/blob/master/lib/unix_write.c


thanks for the pointer, but I was hoping to pull this off remaining
entirely on the Scheme side

I don't know if that will be possible, tough; now I have a new poblem, but
I'm gonna open a new thread for that

Thanks again


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: segmenation fault
  2017-06-09  8:00   ` Amirouche Boubekki
  2017-06-09  8:17     ` Catonano
@ 2017-06-11 20:56     ` Catonano
  2017-06-11 22:09       ` Catonano
  1 sibling, 1 reply; 10+ messages in thread
From: Catonano @ 2017-06-11 20:56 UTC (permalink / raw)
  To: Amirouche Boubekki; +Cc: guile-user

Amirouche, Thomas,


2017-06-09 10:00 GMT+02:00 Amirouche Boubekki <amirouche.boubekki@gmail.com>
:

>  It's something like (bytevector->pointer (make-vector)) there is a
> make-double-pointer procedure in guile git
>

I have overlooked both your remarks and Thomas's remarks

I apologize, I was not understanding the issue correctly

I have found define-wrapped-pointer-type in the manual only now and of
course both of you are right.

The case described in the manual is slightly different than the one freexl
poses

I can't wrap a pointer created by the C funtion. I have to pass one created
in scheme land

The make-double-pointer  procedure in guile-git seems appropriate. Thanks,
Amirouche


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: segmenation fault
  2017-06-11 20:56     ` Catonano
@ 2017-06-11 22:09       ` Catonano
  0 siblings, 0 replies; 10+ messages in thread
From: Catonano @ 2017-06-11 22:09 UTC (permalink / raw)
  To: Amirouche Boubekki; +Cc: guile-user, Matt Wette

2017-06-11 22:56 GMT+02:00 Catonano <catonano@gmail.com>:

> Amirouche, Thomas,
>
>
> 2017-06-09 10:00 GMT+02:00 Amirouche Boubekki <
> amirouche.boubekki@gmail.com>:
>
>>  It's something like (bytevector->pointer (make-vector)) there is a
>> make-double-pointer procedure in guile git
>>
>
> I have overlooked both your remarks and Thomas's remarks
>
> I apologize, I was not understanding the issue correctly
>
> I have found define-wrapped-pointer-type in the manual only now and of
> course both of you are right.
>
> The case described in the manual is slightly different than the one freexl
> poses
>
> I can't wrap a pointer created by the C funtion. I have to pass one
> created in scheme land
>
> The make-double-pointer  procedure in guile-git seems appropriate. Thanks,
> Amirouche
>


Ok, I finally managed to make it work !

Amirouche, I didn't remember but I had written a line exactly equal to
yours:
(bytevector->pointer (make-bytevector (sizeof '*)))

in order to create e void pointer in scheme land !

The thing is that when I passed it in as an argument it was a void pointer:
void *

but when freexl_open had run it was a double pointer: handler **

Subsequent functions expected a simple pointer: handler *

So it simply had to be dereferenced ! Like this

(dereference-pointer handler **double-pointer)

this returns a handler *single-pointer

It's impossible discussing pointer types without becoming psychedelic in a
few lines ! :-/

Now, to make thing really tidy, I should also use the wrap unwrap mechanism

Thank you all people !


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2017-06-11 22:09 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-06-09  7:21 segmenation fault Catonano
2017-06-09  7:43 ` Thomas Danckaert
2017-06-09  8:00   ` Amirouche Boubekki
2017-06-09  8:17     ` Catonano
2017-06-09 11:18       ` Chris Vine
2017-06-09 16:36         ` Josh Datko
2017-06-09 20:18         ` Catonano
2017-06-11 20:56     ` Catonano
2017-06-11 22:09       ` Catonano
2017-06-09  8:42   ` Catonano

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).