[m-users.] Memory management confusion
Zoltan Somogyi
zoltan.somogyi at runbox.com
Sun Apr 20 04:05:40 AEST 2025
On Sat, 19 Apr 2025 19:43:27 +0200, Volker Wysk <post at volker-wysk.de> wrote:
> Hi.
>
> I'm a little confused about memory management. I've read "Memory management
> for C" in the Language Reference Manual and the comments in
> mercury_memory.h.
>
> In there is this:
>
> // These routines all allocate memory that will be traced by the
> // conservative garbage collector, if conservative GC is enabled.
> // (For the native GC, you need to call MR_add_root() to register roots.)
> // These routines all check for a null return value themselves,
> // so the caller need not check.
>
> But what happens when such a null return value is detected?
Looking at the code of those functions in mercury_memory.c will tell you:
they abort the program. There is nothing you can do to recover from
such an error; the best you can do is avoid making the problem worse.
> And one of "these routines" (see above) is MR_GC_NEW. In lines 1028/1029 of
> the ODBC extra library is this:
>
> // Doing manual deallocation of the statement object.
> Statement = MR_GC_NEW(MODBC_Statement);
>
> According to mercury_memory.h, memory allocated by MR_GC_NEW is GC-traced.
> This means that it is registered for later deallocation as well as searched
> for pointers to other registered memory blocks. It *does* get garbage
> collected. Why does the comment say "Doing manual deallocation"? Is this a
> documentation error? Or what could be meant by that comment?
It seems to be a bad comment. It was already present in the original commit
of that file from 1997, so I am pretty sure that the original author would not
be able to remember why he wrote that. Can you remember why *you* wrote
a given line of code almost three decades ago?
Zoltan.
More information about the users
mailing list