<html><head></head><body><div class="gmail_quote">Am 1. Mai 2015 14:23:49 MEZ, schrieb Mark Brown <mark@mercurylang.org>:<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<pre class="k9mail">On Fri, May 1, 2015 at 5:54 PM, Peter Wang <novalazy@gmail.com> wrote:<br /><blockquote class="gmail_quote" style="margin: 0pt 0pt 1ex 0.8ex; border-left: 1px solid #729fcf; padding-left: 1ex;"> On Fri, 01 May 2015 07:02:48 +0100, Matthias Guedemann <matthias.guedemann@googlemail.com> wrote:<br /><blockquote class="gmail_quote" style="margin: 0pt 0pt 1ex 0.8ex; border-left: 1px solid #ad7fa8; padding-left: 1ex;"> Hi Peter,<br /><br /><blockquote class="gmail_quote" style="margin: 0pt 0pt 1ex 0.8ex; border-left: 1px solid #8ae234; padding-left: 1ex;"> You should allocate the mp_ints like this:<br /></blockquote><br /><blockquote class="gmail_quote" style="margin: 0pt 0pt 1ex 0.8ex; border-left: 1px solid #8ae234; padding-left: 1ex;"> Mp_Int = MR_GC_NEW_ATTRIB(mp_int, MR_ALLOC_ID);<br /></blockquote><br /><blockquote class="gmail_quote" style="margin: 0pt 0pt 1ex 0.8ex; border-left: 1px solid #8ae234; padding-left: 1ex;"> For the string in
mp_to_string you should use<br /></blockquote><br /><blockquote class="gmail_quote" style="margin: 0pt 0pt 1ex 0.8ex; border-left: 1px solid #8ae234; padding-left: 1ex;"> MR_allocate_aligned_string_msg(S, length, MR_ALLOC_ID);<br /></blockquote><br /><blockquote class="gmail_quote" style="margin: 0pt 0pt 1ex 0.8ex; border-left: 1px solid #8ae234; padding-left: 1ex;"> or other macros in mercury_string.h<br /></blockquote><br /> ok, thank you for the hints. Could you point me to the relevant section<br /> in the user guide, or is the documentation only in the source files?<br /></blockquote><br /> Unfortunately the latter (mercury_memory.h and mercury_string.h).<br /> I think no memory management interface has been documented because<br /> the existing interface is specific to conservative GCs, if not Boehm<br /> GC. Nonetheless, practical code using the foreign language interface<br /> invariably needs to allocate so I think we should document some of the<br /> macros and
functions, with caveats.<br /></blockquote><br />That's done for lists and floats (section 14.3.1), and ought to be<br />done for strings, too. Allocating foreign types on Mercury's heap<br />needs one big caveat, though, namely that no finaliser will be called.<br />If the foreign type needs to be explicitly freed, allocating it on<br />Mercury's heap won't work.<br /><br />Unfortunately, libtommath falls into this category - it expects<br />mp_ints to be cleared when they're done with. So the code still leaks<br />even with the above changes.<br /><br />Cheers,<br />Mark.<br /><hr /><br />users mailing list<br />users@lists.mercurylang.org<br /><a href="https://www.mercurylang.org/lists/listinfo/users">https://www.mercurylang.org/lists/listinfo/users</a><br /></pre></blockquote></div><br clear="all">Hi Mark,<br>
<br>
thanks for the info. I'll add predicates to clear mp_int values which call the finalizers.<br>
<br>
Is this because mp_int is just a struct containing pointers to malloc'ed memory? So then the GC frees the memory malloc'ed for mp_int itself, and the internal stays stay allocated?<br>
<br>
Matthias</body></html>