Thanks for your help & 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>
"The result must be deallocated with xmlFree()"<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 "xmlreader.h xmlTextReaderValue"<br> c_xmlTextReaderValue :: Ptr XmlTextReader -> IO (CString)<br>value :: (MonadIO m) => XmlReader -> m (Maybe B.ByteString)<br>value (XmlReader reader_fp) = liftIO $<br>
withForeignPtr reader_fp $ \reader_ptr -> do<br> cstr <- c_xmlTextReaderValue reader_ptr<br> fromPtr B.packCString cstr c_xmlFree<br><br>fromPtr :: (Ptr a -> IO b) -> Ptr a -> (Ptr a -> IO ()) -> IO (Maybe b)<br>
fromPtr c2h ptr free | ptr == nullPtr = return Nothing<br> | otherwise = do<br> r <- 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 "xmlreader.h xmlFree"<br>
c_xmlFree :: Ptr a -> 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"><<a href="mailto:duncan.coutts@googlemail.com">duncan.coutts@googlemail.com</a>></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>
> Hello,<br>
> I get a segfault when I do<br>
> str <- peekCString ptr<br>
> free ptr<br>
> return (Just str)<br>
<br>
</div>As Thomas says, you've not really given us enough info here.<br>
<br>
I'll make some guesses and observations anyway :-)<br>
<div class="im"><br>
> But not when I don'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>
> I also get the same behaviour with ByteString.packCString...<br>
<br>
</div>That'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>
> Could you please tell me if the memory is correctly freed by GHC when<br>
> I don't do it myself?<br>
> And how can I specify a custom free function (i.e. xmlFree function in<br>
> libxml2)?<br>
<br>
</div>See the documentation for ForeignPtr.<br>
<div class="im"><br>
> Maybe I should use a data type with two fields : the String/ByteString<br>
> 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>