<p>Thanks to Valgrind, I found my mistake. I made a vector with size two, then put three items into it. Specifically, the root node went into index 2, which is why that field of the xml-document was the one with the bug. That silly problem fixed, things seem to be working fine. But if anybody has a spare minute and would be willing to check over my code, I would still appreciate any feedback about whether I'm doing things the right way. Especially when it comes to memory management. Thanks! </p>

<p>Sent from my mobile. </p>
<div class="gmail_quote">On Feb 9, 2012 6:33 PM, "Taylor Venable" <<a href="mailto:taylor@metasyntax.net">taylor@metasyntax.net</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hail, brothers and sisters in Scheme.<br>
<br>
I've been working with Gambit's FFI recently, trying to create<br>
bindings for libxml2. Currently I'm trying to use libxml2 to parse a<br>
file, and produce structures representing the XML nodes. Reading<br>
Gambit's lib/os.c for some examples of how to produce such things (I<br>
started with service-info and worked down from there) I developed some<br>
code that pretty much works. But there are problems with memory<br>
corruption that result in "odd" looking results (for example, a list<br>
that should have some 95,000 items is instead two large fixnums consed<br>
together) or segfaults. Finally I teased out an XML file that fails<br>
pretty reliably, at least on my desktop.<br>
<br>
First, the code is on Bitbucket ---<br>
<a href="https://bitbucket.org/taylor_venable/gambit-xml/" target="_blank">https://bitbucket.org/taylor_venable/gambit-xml/</a>. The default Makefile<br>
target will build xml.o1 - you will probably have to invoke it as<br>
"make GSC=gsc" (the Makefile uses "GSC=gambitc" by default, which is<br>
the name used by the Gambit compiler on Arch Linux since the "gsc"<br>
program is from Ghostscript).<br>
<br>
Second, the test XML file is on my website ---<br>
<a href="http://metasyntax.net/test.xml.gz" target="_blank">http://metasyntax.net/test.xml.gz</a> --- warning about clicking that in<br>
your browser, it's 141 MB uncompressed.<br>
<br>
Here's how I'm using things:<br>
<br>
$ make<br>
gcc -c -o os_xml.o -I/usr/include/libxml2 -g -O0 -Wall -Wno-unused<br>
-fPIC os_xml.c<br>
gambitc -debug -link -flat -o xml.o1.c xml.scm<br>
*** WARNING -- "##direct-structure-ref" is not defined,<br>
***            referenced in: ("/home/taylor/Repos/gambit-xml/xml.c")<br>
*** WARNING -- "##direct-structure-set!" is not defined,<br>
***            referenced in: ("/home/taylor/Repos/gambit-xml/xml.c")<br>
*** WARNING -- "##structure" is not defined,<br>
***            referenced in: ("/home/taylor/Repos/gambit-xml/xml.c")<br>
*** WARNING -- "##structure-instance-of?" is not defined,<br>
***            referenced in: ("/home/taylor/Repos/gambit-xml/xml.c")<br>
*** WARNING -- "##structure-type-set!" is not defined,<br>
***            referenced in: ("/home/taylor/Repos/gambit-xml/xml.c")<br>
*** WARNING -- "##subtype-set!" is not defined,<br>
***            referenced in: ("/home/taylor/Repos/gambit-xml/xml.c")<br>
*** WARNING -- "error" is not defined,<br>
***            referenced in: ("/home/taylor/Repos/gambit-xml/xml.c")<br>
*** WARNING -- "fx=" is not defined,<br>
***            referenced in: ("/home/taylor/Repos/gambit-xml/xml.c")<br>
*** WARNING -- "map" is not defined,<br>
***            referenced in: ("/home/taylor/Repos/gambit-xml/xml.c")<br>
*** WARNING -- "vector-ref" is not defined,<br>
***            referenced in: ("/home/taylor/Repos/gambit-xml/xml.c")<br>
gcc -c -o xml.o -I/usr/include/libxml2 -D___DYNAMIC -g -O0 -Wall<br>
-Wno-unused -fPIC xml.c<br>
gcc -c -o xml.o1.o -I/usr/include/libxml2 -D___DYNAMIC -g -O0 -Wall<br>
-Wno-unused -fPIC xml.o1.c<br>
gcc -shared -o xml.o1 -lxml2 os_xml.o xml.o xml.o1.o<br>
$ gsi<br>
Gambit v4.6.3<br>
<br>
> (load "xml")<br>
"/home/taylor/Repos/gambit-xml/xml.o1"<br>
> (define xml (read-xml "./test.xml"))<br>
> (length (xml-element-children (xml-document-root xml)))<br>
Segmentation fault<br>
<br>
Sometimes I can do this:<br>
<br>
$ gsi<br>
Gambit v4.6.3<br>
<br>
> (load "xml")<br>
"/home/taylor/Repos/gambit-xml/xml.o1"<br>
> (define xml (read-xml "./test.xml"))<br>
> (length (xml-element-children (xml-document-root xml)))<br>
*** ERROR IN (console)@4.9 -- (Argument 1) Instance of #<type #2<br>
xml-element> expected<br>
(xml-element-children '#<gc-hash-table #3>)<br>
1><br>
> xml<br>
#<xml-document #4 location: "./test.xml" root: #<gc-hash-table #3>><br>
<br>
Or this:<br>
<br>
$ gsi<br>
Gambit v4.6.3<br>
<br>
> (load "xml")<br>
"/home/taylor/Repos/gambit-xml/xml.o1"<br>
> (define xml (read-xml "/home/taylor/sis-conv-input.xml"))<br>
> (length (xml-element-children (xml-document-root xml)))<br>
*** ERROR IN (console)@3.1 -- (Argument 1) LIST expected<br>
(length '(23220778 . 22958618))<br>
1> xml<br>
#<xml-document #2<br>
   location: "/home/taylor/sis-conv-input.xml"<br>
   root: #<xml-element #3 name: "enterprise" attrs: () children:<br>
(23220778 . 22958618)>><br>
<br>
It varies. Sometimes I get the error because children is a something<br>
like '(23220778 . 22958618), then when I print out the xml value, the<br>
root is printed as #<gc-hash-table #3>. So it looks to me like a<br>
memory corruption problem, but I don't know why. When I use gdb to<br>
look for a cause:<br>
<br>
$ gdb gsi<br>
GNU gdb (GDB) 7.4<br>
Copyright (C) 2012 Free Software Foundation, Inc.<br>
License GPLv3+: GNU GPL version 3 or later <<a href="http://gnu.org/licenses/gpl.html" target="_blank">http://gnu.org/licenses/gpl.html</a>><br>
This is free software: you are free to change and redistribute it.<br>
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"<br>
and "show warranty" for details.<br>
This GDB was configured as "x86_64-unknown-linux-gnu".<br>
For bug reporting instructions, please see:<br>
<<a href="http://www.gnu.org/software/gdb/bugs/" target="_blank">http://www.gnu.org/software/gdb/bugs/</a>>...<br>
Reading symbols from /usr/bin/gsi...(no debugging symbols found)...done.<br>
(gdb) run<br>
Starting program: /usr/bin/gsi<br>
Gambit v4.6.3<br>
<br>
> (load "xml")<br>
"/home/taylor/Repos/gambit-xml/xml.o1"<br>
> (define xml (read-xml "./test.xml"))<br>
> (length (xml-element-children (xml-document-root xml)))<br>
<br>
Program received signal SIGSEGV, Segmentation fault.<br>
0x00007ffff702b064 in ___H_xml_2d_element_2d_children (___ps=0xa4ce20)<br>
at xml.c:6232<br>
6232       ___IF(___STRUCTUREDIOP(___R1,___R2))<br>
(gdb) bt<br>
#0  0x00007ffff702b064 in ___H_xml_2d_element_2d_children<br>
(___ps=0xa4ce20) at xml.c:6232<br>
#1  0x00000000004ad51f in ___call ()<br>
#2  0x00000000004af14f in ___setup ()<br>
#3  0x00000000004c3ef2 in ___main ()<br>
#4  0x00000000004b8436 in ___main_char ()<br>
#5  0x00000000004ac6a9 in main ()<br>
(gdb) print ___ps->r<br>
$1 = {10259873, 11115697, 140737339703681, 11115697, 140737339703681}<br>
<br>
The problem manifests in code generated by Gambit, but I doubt that's<br>
where the problem originates. I don't know enough about the Gambit<br>
internals to make it any further on my own. I hope somebody can help<br>
guide me in debugging what's wrong, and hopefully point out whether my<br>
memory handling in the os_xml.c file is correct or not. There are a<br>
few places where I have doubts about whether I should use<br>
___release_scmobj() or not.<br>
<br>
All of these problems happen fairly regularly on my desktop, which is<br>
an Intel Core i7-870 with 8GB of memory running Arch Linux x86_64<br>
kernel 3.2.4 --- but I haven't yet had them on my laptop, which is an<br>
Intel Core 2 Duo P8800 with 4GB of memory running the same kernel.<br>
<br>
Mighty thanks for any assistance!<br>
<br>
--<br>
Taylor C. Venable<br>
<a href="http://metasyntax.net/" target="_blank">http://metasyntax.net/</a><br>
</blockquote></div>