Thanks for your help &amp; time,<br><br>I checked the C API documentation:<br><a href="http://xmlsoft.org/html/libxml-xmlreader.html#xmlTextReaderValue">http://xmlsoft.org/html/libxml-xmlreader.html#xmlTextReaderValue</a><br>
&quot;The result must be deallocated with xmlFree()&quot;<br><br>I pushed the sources here:<br><a href="http://github.com/gwenn/libxml-reader">http://github.com/gwenn/libxml-reader</a><br><br>My problem is with the function at line 249 in Text.XML.LibXML.XmlReader<br>
To make it work, I have to comment out the line where xmlFree is called.<br><br>-- xmlChar * xmlTextReaderValue(xmlTextReaderPtr reader)<br>-- Returns:the string or NULL if not available. The result must be deallocated with xmlFree()<br>
foreign import ccall unsafe &quot;xmlreader.h xmlTextReaderValue&quot;<br>    c_xmlTextReaderValue :: Ptr XmlTextReader -&gt; IO (CString)<br>value :: (MonadIO m) =&gt; XmlReader -&gt; m (Maybe B.ByteString)<br>value (XmlReader reader_fp) = liftIO $<br>
    withForeignPtr reader_fp $ \reader_ptr -&gt; do<br>        cstr &lt;- c_xmlTextReaderValue reader_ptr<br>        fromPtr B.packCString cstr c_xmlFree<br><br>fromPtr :: (Ptr a -&gt; IO b) -&gt; Ptr a -&gt; (Ptr a -&gt; IO ()) -&gt; IO (Maybe b)<br>
fromPtr c2h ptr free | ptr == nullPtr = return Nothing<br>                     | otherwise = do<br>                        r &lt;- c2h ptr<br>                        free ptr -- FIXME<br>                        return (Just r)<br>
<br>May be the binding to xmlFree is wrong?<br>foreign import ccall unsafe &quot;xmlreader.h xmlFree&quot;<br>
    c_xmlFree :: Ptr a -&gt; IO ()<br>
<br>To test, you will need libxml2-dev:<br>$ runhaskell Setup configure --extra-include-dirs=/usr/include/libxml2/<br>$ runhaskell Setup build<br>$ ./dist/build/Test/Test<br><br>C functions are described here:<br><a href="http://xmlsoft.org/html/libxml-xmlreader.html">http://xmlsoft.org/html/libxml-xmlreader.html</a><br>
And here:<br><a href="http://xmlsoft.org/html/libxml-globals.html">http://xmlsoft.org/html/libxml-globals.html</a><br><br>Regards.<br><br><div class="gmail_quote">On Sun, Nov 29, 2009 at 7:02 PM, Duncan Coutts <span dir="ltr">&lt;<a href="mailto:duncan.coutts@googlemail.com">duncan.coutts@googlemail.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div class="im">On Sun, 2009-11-29 at 18:00 +0100, El Barto wrote:<br>
&gt; Hello,<br>
&gt; I get a segfault when I do<br>
&gt; str &lt;- peekCString ptr<br>
&gt; free ptr<br>
&gt; return (Just str)<br>
<br>
</div>As Thomas says, you&#39;ve not really given us enough info here.<br>
<br>
I&#39;ll make some guesses and observations anyway :-)<br>
<div class="im"><br>
&gt; But not when I don&#39;t free the C pointer.<br>
<br>
</div>Are you sure the C code does not also free the memory? That would lead<br>
to a double-free which can easily manifest as a segfault.<br>
<div class="im"><br>
&gt; I also get the same behaviour with ByteString.packCString...<br>
<br>
</div>That&#39;s pretty odd since packCString does not free the C string, it makes<br>
a copy. I suppose it could possibly segfault if your C string was not<br>
actually null terminated (as C strings must be).<br>
<br>
There are also variants that do not copy, such as<br>
unsafePackMallocCString. See the docs for a description.<br>
<div class="im"><br>
&gt; Could you please tell me if the memory is correctly freed by GHC  when<br>
&gt; I don&#39;t do it myself?<br>
&gt; And how can I specify a custom free function (i.e. xmlFree function in<br>
&gt; libxml2)?<br>
<br>
</div>See the documentation for ForeignPtr.<br>
<div class="im"><br>
&gt; Maybe I should use a data type with two fields : the String/ByteString<br>
&gt; and the ForeignPtr to the CString?<br>
<br>
</div>Is your C String supposed to be freed using the C free() function, or is<br>
it supposed to be freed using xmlFree() or something?<br>
<font color="#888888"><br>
Duncan<br>
<br>
</font></blockquote></div><br>