Hi there,
I'm trying to use cffi to write some bindings for libsane. Having quite some success, but for the following (rather bizarre) problem. libsane expects: sane_init(), do some stuff, sane_exit() but when I call sane_exit after having done anything non-trivial (for example, called sane_get_devices), sbcl segfaults and dies.
I realise that that's not the sort of question for the cffi list, _but_ I wrote a trivial C program that I think should be equivalent and it doesn't die. I can't work out *how* libsane can work in one case and not the other (!) so I was wondering if anyone could make any suggestions. I'm attaching the two source files here.
The other piece of information is that I asked about this on #lisp a few minutes ago, and hefner very kindly tested it out, with no segfault at his end. I've had the same symptom with both the asdf-installed cffi and the darcs lotsastuff branch. I presume there's an error somewhere in my libsane config which explains why it's dying, but I'm hoping that someone can explain how it can fail in the lisp program and not in the C one.
Oh, it's linux i386 with SBCL from CVS this evening.
Thanks,
Rupert
On Thu, May 22, 2008 at 12:01 AM, Rupert Swarbrick rswarbrick@googlemail.com wrote:
I realise that that's not the sort of question for the cffi list, _but_ I wrote a trivial C program that I think should be equivalent and it doesn't die. I can't work out *how* libsane can work in one case and not the other (!) so I was wondering if anyone could make any suggestions. I'm attaching the two source files here.
The only difference I could spot between the C and Lisp programs is that version should be an :int, not a :pointer. (But that doesn't matter on 32-bit linux since they're the same size there.) In any case, it seems to work for me on Linux/amd64.
Have you tried running the program in other Lisps? CLISP and/or CMUCL, for instance.
"Luís Oliveira" luismbo@gmail.com writes:
On Thu, May 22, 2008 at 12:01 AM, Rupert Swarbrick rswarbrick@googlemail.com wrote:
I realise that that's not the sort of question for the cffi list, _but_ I wrote a trivial C program that I think should be equivalent and it doesn't die. I can't work out *how* libsane can work in one case and not the other (!) so I was wondering if anyone could make any suggestions. I'm attaching the two source files here.
The only difference I could spot between the C and Lisp programs is that version should be an :int, not a :pointer. (But that doesn't matter on 32-bit linux since they're the same size there.) In any case, it seems to work for me on Linux/amd64.
Have you tried running the program in other Lisps? CLISP and/or CMUCL, for instance.
Hi there,
Thanks for the reply and sorry for the delay - I've been doing exams. I have now tried with clisp and cmucl and they both segfault. This is with the debian unstable versions of clisp, cmucl, cl-cffi.
I presume there's something weird happening with my system's libsane library, which would explain why no-one else's versions die, but I would like to try and work out why the lisp versions are crashing and the trivial C version not.
However, I tried starting up sbcl with gdb and it was a bit too unpleasant - gdb reported about 5 [1] segfaults per line in the repl, but everything carried on working (well until I loaded my test2.lisp file [2]).
I'd be really interested to debug/analyse what's going on here - there must be a problem somewhere! Can anyone recommend ways in which I can somehow trap whatever's falling over and catch the segfault? Or suggest how I might find what's going wrong? I have no idea who's bug this is, but I'd like to understand why I can trigger it so reliably!
Thank you very much for any suggestions,
Rupert
[1] Er this was the other night. That's a rough estimate. [2] Included below. Dies every time.
On Mon, May 26, 2008 at 9:23 PM, Rupert Swarbrick rswarbrick@googlemail.com wrote:
However, I tried starting up sbcl with gdb and it was a bit too unpleasant - gdb reported about 5 [1] segfaults per line in the repl, but everything carried on working (well until I loaded my test2.lisp file [2]).
http://sbcl-internals.cliki.net/gdb contains some (possibly outdated) tips. Also, I noticed that libsane seems to have a lot of debug statements along its code. I suggest you build a version of libsane with those enabled. Since your program is segfaulting very quickly, that should help in identifying the problem.
"Luís Oliveira" luismbo@gmail.com writes:
On Mon, May 26, 2008 at 9:23 PM, Rupert Swarbrick rswarbrick@googlemail.com wrote:
However, I tried starting up sbcl with gdb and it was a bit too unpleasant - gdb reported about 5 [1] segfaults per line in the repl, but everything carried on working (well until I loaded my test2.lisp file [2]).
http://sbcl-internals.cliki.net/gdb contains some (possibly outdated) tips. Also, I noticed that libsane seems to have a lot of debug statements along its code. I suggest you build a version of libsane with those enabled. Since your program is segfaulting very quickly, that should help in identifying the problem.
Thank you very much for the help. I'm going to try that tonight. Sorry for the late reply - programming got put on hold rather while I sat some university exams.
Anyway, I'll see if I can work out what's going on and hopefully let you know.
Rupert
Rupert Swarbrick rswarbrick@googlemail.com writes:
The other piece of information is that I asked about this on #lisp a few minutes ago, and hefner very kindly tested it out, with no segfault at his end.
Same SBCL and CFFI?
(with-foreign-object (version :pointer)
Check the w-f-o doc; this should probably be :int.
Haha. I finally worked out that the issue was with one of the patches applied to the sources for the debian package, worked out which one, and emailed the author. What I didn't work out until he replied was that he'd already fixed the problem, and the new version of the package had been in debian unstable for a couple of days. D'oh!
Anyway, thanks everybody for the helpful comments.
Rupert