[m-users.] Segmentation violation on FFI wrapper for linenoise-ng

Zoltan Somogyi zoltan.somogyi at runbox.com
Sat Jun 19 21:22:27 AEST 2021


2021-06-19 20:57 GMT+10:00 "Sean Charles (emacstheviking)" <objitsu at gmail.com>:
> Does Mercury have something to with this e.g. a SIGINT handler or something 

Yes, Mercury does set signal handlers, though whether they are involved
in this problem is a different question. Have a look at runtime/mercury_signal.[ch],
and at the code in other source files that use that module's functionality.

>     % ln_free: Release a previosuly read line.
>     % In order to prevent memory leaks, after the REPL has dealt with the
>     % user input this must be called to hand the memory back to the pool.
>     %
> :- pragma foreign_proc(
>     "C",
>     ln_free(Line::in, IOin::di, IOout::uo),
>     [will_not_throw_exception, promise_pure],
>     "
>     free(Line);
>     IOout = IOin;
>     "
> ).

Mercury does NOT use malloc to allocate memory for strings.
If you call free to try to deallocate a string that was allocated by Mercury,
a SIGSEGV would be far from surprising.

In general, you must not try to deallocate anything allocated by Mercury;
instead, you let the Boehm collector collect it. (The gc.m module in
the Mercury standard library has a predicate that asks for an immediate
collection, but that is quite different from collecting one memory block.)

Zoltan.


More information about the users mailing list