<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">Hi Zoltan,<div class=""><br class=""></div><div class="">I will check those files for sure. The memory I am freeing was allocated within C world by the linenoise-ng library not by Mercury, I am merely following best practice on that, <a href="https://github.com/antirez/linenoise" class="">https://github.com/antirez/linenoise</a> ...</div><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre"> </span>><span style="caret-color: rgb(36, 41, 46); color: rgb(36, 41, 46); background-color: rgb(255, 255, 255);" class="">The returned line should be freed with the </span><code style="box-sizing: border-box; padding: 0.2em 0.4em; margin: 0px; border-top-left-radius: 6px; border-top-right-radius: 6px; border-bottom-right-radius: 6px; border-bottom-left-radius: 6px; caret-color: rgb(36, 41, 46); color: rgb(36, 41, 46);" class="">free()</code><span style="caret-color: rgb(36, 41, 46); color: rgb(36, 41, 46); background-color: rgb(255, 255, 255);" class=""> standard system call. However sometimes it could happen that your program uses a different dynamic allocation library, so you may also used </span><code style="box-sizing: border-box; padding: 0.2em 0.4em; margin: 0px; border-top-left-radius: 6px; border-top-right-radius: 6px; border-bottom-right-radius: 6px; border-bottom-left-radius: 6px; caret-color: rgb(36, 41, 46); color: rgb(36, 41, 46);" class="">linenoiseFree</code><span style="caret-color: rgb(36, 41, 46); color: rgb(36, 41, 46); background-color: rgb(255, 255, 255);" class=""> to make sure the line is freed with the same allocator it was created.</span></div><div class=""><font color="#24292e" class=""><span style="caret-color: rgb(36, 41, 46); background-color: rgb(255, 255, 255);" class=""><br class=""></span></font></div><div class=""><font color="#24292e" class=""><span style="caret-color: rgb(36, 41, 46); background-color: rgb(255, 255, 255);" class="">For whatever reason (I checked the header) the linenoise-ng variant that supports UTF-8 does not provide linenoiseFree() so I used free() as shown in the orginal example.c</span></font></div><div class=""><font color="#24292e" class=""><span style="caret-color: rgb(36, 41, 46); background-color: rgb(255, 255, 255);" class=""><br class=""></span></font></div><div class=""><font color="#24292e" class=""><span style="caret-color: rgb(36, 41, 46); background-color: rgb(255, 255, 255);" class="">INTERESTING: I played around a little and discovered via some blunderbuss printf() in the C code that it ONLY segfaults if I use CTRL-D as soon as the repl starts:</span></font></div><div class=""><font color="#24292e" class=""><span style="caret-color: rgb(36, 41, 46); background-color: rgb(255, 255, 255);" class=""><br class=""></span></font></div><div class=""><font color="#24292e" class=""><span style="background-color: rgb(255, 255, 255);" class=""><div class="">{13:50}~/Documents/code/mercury/f2:ast-start ✗ ➭ ./felt -r</div><div style="font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji";" class=""><br class=""></div><div style="font-size: medium;" class=""><font face="Courier New" class="">FELT ➭ Version: v3.14.0 - Sean Charles 2021</font></div><div style="font-size: medium;" class=""><font face="Courier New" class="">Be prepared for a most excellent journey....</font></div><div style="font-size: medium;" class=""><font face="Courier New" class=""><br class=""></font></div><div style="font-size: medium;" class=""><font face="Courier New" class="">FELT ➭ <b class="">**PRESSED CTRLD-D HERE**</b></font></div><div style="font-size: medium;" class=""><font face="Courier New" class=""><br class=""></font></div><div style="font-size: medium;" class=""><font face="Courier New" class="">*** Mercury runtime: caught segmentation violation ***</font></div><div style="font-size: medium;" class=""><font face="Courier New" class="">cause: address not mapped to object</font></div><div style="font-size: medium;" class=""><font face="Courier New" class="">address involved: 0x0</font></div><div style="font-size: medium;" class=""><font face="Courier New" class="">This may have been caused by a stack overflow, due to unbounded recursion.</font></div><div style="font-size: medium;" class=""><font face="Courier New" class="">exiting from signal handler</font></div><div style="font-size: medium;" class=""><font face="Courier New" class="">[1] 70777 segmentation fault ./felt -r</font></div></span></font></div><div class=""><font color="#24292e" face="-apple-system, BlinkMacSystemFont, Segoe UI, Helvetica, Arial, sans-serif, Apple Color Emoji, Segoe UI Emoji" size="3" class=""><span style="caret-color: rgb(36, 41, 46); background-color: rgb(255, 255, 255);" class=""><br class=""></span></font></div><div class=""><font color="#24292e" face="-apple-system, BlinkMacSystemFont, Segoe UI, Helvetica, Arial, sans-serif, Apple Color Emoji, Segoe UI Emoji" size="3" class=""><span style="caret-color: rgb(36, 41, 46); background-color: rgb(255, 255, 255);" class="">If I run it again and press RETURN on a blank line then pressing CTRL-D does not cause the issue:</span></font></div><div class=""><font color="#24292e" face="-apple-system, BlinkMacSystemFont, Segoe UI, Helvetica, Arial, sans-serif, Apple Color Emoji, Segoe UI Emoji" size="3" class=""><span style="caret-color: rgb(36, 41, 46); background-color: rgb(255, 255, 255);" class=""><br class=""></span></font></div><div class=""><font color="#24292e" size="3" class=""><span style="background-color: rgb(255, 255, 255);" class=""><div class=""><font face="Courier New" class="">{13:53}~/Documents/code/mercury/f2:ast-start ✗ ➭ ./felt -r</font></div><div class=""><font face="Courier New" class=""><br class=""></font></div><div class=""><font face="Courier New" class="">FELT ➭ Version: v3.14.0 - Sean Charles 2021</font></div><div class=""><font face="Courier New" class="">Be prepared for a most excellent journey....</font></div><div class=""><font face="Courier New" class=""><br class=""></font></div><div class=""><font face="Courier New" class="">FELT ➭ <b class="">**PRESSED RETURN**</b></font></div><div class=""><font face="Courier New" class="">LNG: NULL:0, len:0, </font></div><div class=""><font face="Courier New" class="">FELT ➭ <b class="">**PRESSED CTRL-D**</b></font></div><div class=""><font face="Courier New" class="">LNG: NULL:1, len:0, </font></div><div class=""><font face="Courier New" class=""><br class=""></font></div><div class=""><font face="Courier New" class="">FELT ➭ Be excellent to one another.</font></div><div style="font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji";" class=""><br class=""></div><div style="font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji";" class=""><br class=""></div><div style="font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji";" class="">This smells! :D. Not sure which variety of fish it is yet though. I suspect I MAY have found some edge case bug in the the library, fortunately for me I built it from source and am more than comfortable with C and C++ so I will focus on that later this weekend but for now I may continue with my project once again.</div><div style="font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji";" class=""><br class=""></div><div style="font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji";" class="">Thanks for your help. I took onboard your previous comments about not throwing exceptions and my ast builder is taking shape too.</div><div style="font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji";" class=""><br class=""></div><div style="font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji";" class="">Sean.</div><div style="font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji";" class=""><br class=""></div><div style="font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji";" class=""><br class=""></div></span></font><div><br class=""><blockquote type="cite" class=""><div class="">On 19 Jun 2021, at 12:22, Zoltan Somogyi <<a href="mailto:zoltan.somogyi@runbox.com" class="">zoltan.somogyi@runbox.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class=""><br class="">2021-06-19 20:57 GMT+10:00 "Sean Charles (emacstheviking)" <<a href="mailto:objitsu@gmail.com" class="">objitsu@gmail.com</a>>:<br class=""><blockquote type="cite" class="">Does Mercury have something to with this e.g. a SIGINT handler or something <br class=""></blockquote><br class="">Yes, Mercury does set signal handlers, though whether they are involved<br class="">in this problem is a different question. Have a look at runtime/mercury_signal.[ch],<br class="">and at the code in other source files that use that module's functionality.<br class=""><br class=""><blockquote type="cite" class=""> % ln_free: Release a previosuly read line.<br class=""> % In order to prevent memory leaks, after the REPL has dealt with the<br class=""> % user input this must be called to hand the memory back to the pool.<br class=""> %<br class="">:- pragma foreign_proc(<br class=""> "C",<br class=""> ln_free(Line::in, IOin::di, IOout::uo),<br class=""> [will_not_throw_exception, promise_pure],<br class=""> "<br class=""> free(Line);<br class=""> IOout = IOin;<br class=""> "<br class="">).<br class=""></blockquote><br class="">Mercury does NOT use malloc to allocate memory for strings.<br class="">If you call free to try to deallocate a string that was allocated by Mercury,<br class="">a SIGSEGV would be far from surprising.<br class=""><br class="">In general, you must not try to deallocate anything allocated by Mercury;<br class="">instead, you let the Boehm collector collect it. (The gc.m module in<br class="">the Mercury standard library has a predicate that asks for an immediate<br class="">collection, but that is quite different from collecting one memory block.)<br class=""><br class="">Zoltan.</div></div></blockquote></div><br class=""></div></body></html>