Thanks for the very quick reply—using `_fpointer` seems to work for me,
too. I don't think I'd noticed it in the docs before, though, even if I
had, I don't think it would have been obvious to me that it was the
solution to this problem.

-Philip


On Mon, Aug 27, 2018 at 8:03 AM Ryan Culpepper <ry...@ccs.neu.edu> wrote:

> On 08/27/2018 02:13 PM, Philip McGrath wrote:
> > I am hoping for some help debugging a problem I'm having writing FFI
> > bindings for libxml2.
> >
> > I am trying to use the function `xmlValidateDtd`, which (predictably)
> > validates an XML document against a DTD. To support error reporting, the
> > first argument to the function is a pointer to an `xmlValidCtxt` struct
> > (documented at http://xmlsoft.org/html/libxml-valid.html#xmlValidCtxt,
> > though note that the last two fields are listed twice because whatever
> > extracted the docs didn't understand a processor directive). My wrapper
> > code needs to allocate the `xmlValidCtxt` (via `xmlNewValidCtxt`), set
> > the first field to a `FILE*` for writing obtained via `fopen`, set the
> > second and third fields to pointers to `fprintf`, and then call
> > `xmlValidateDtd` with the instance. I have a little function in C that
> > does this work:
> > https://github.com/LiberalArtist/libxml2-ffi/blob/master/myvalidate.c
> >
> > Of course, I want to write this in Racket, not C, but my attempt to do
> > this via the FFI
> > (https://github.com/LiberalArtist/libxml2-ffi/blob/master/segfault.rkt)
> > causes a segfault. The problem is something in the way I'm initializing
> > the `xmlValidCtxt` struct. If I run my Racket version with a valid
> > document, so that the error reporting isn't used, it works just fine.
> > Likewise, if I don't initialize the first three fields of the
> > `xmlValidCtxt` struct, instead leaving them as null pointers, the
> > default error behavior (writing to standard error) works, though it
> > isn't useful for my purposes. The segfault only happens if I initialize
> > the fields as described and a validation error actually tries to use the
> > fields.
> >
> > I've also confirmed that my C version works. If I compile it to a shared
> > library and load it with the FFI, it works just as desired with both
> > valid and invalid documents:
> > https://github.com/LiberalArtist/libxml2-ffi/blob/master/use-my-so.rkt
> >
> > I don't see anything different between my attempt to initialize the
> > `xmlValidCtxt` struct from Racket and the way I'm doing it in C, but
> > obviously there is some difference. Any debugging suggestions would be
> > appreciated.
>
> It works for me if I change the definition of `_fprintf-ptr` to this:
>
>    (define _fprintf-ptr _fpointer)
>
> The docs say that compared to `_pointer`, `_fpointer` skips a level of
> indirection on symbol lookups. (This also might explain a problem I had
> in the past getting the right value for a symbol whose value is an array
> of pointers to structs. IIRC, I got the pointer to the first struct
> instead.)
>
> Ryan
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to