[mercury-users] Filling Mercury strings in C

Fergus Henderson fjh at cs.mu.OZ.AU
Wed Mar 26 01:42:15 AEDT 2003


On 25-Mar-2003, Peter Ross <pro at missioncriticalit.com> wrote:
> On Tue, Mar 25, 2003 at 01:18:36PM +0100, Petr Nemec wrote:
> > Hello people,
> >  does anybody know (sure he does :),how to fill the Mercury strings
> > in C properly? I use the attached code to read a line and to destroy
> > ending \n in C. But it seems the memory for the allocated strings is
> > not being freed at all during the computation. Do I allocate right :)
> > ?
> 
> You need to use MR_NEW(<type to allocate>) or
> MR_NEW_ARRAY(<type>, <num>) to allocate your memory on the GC'd heap.

No, that's wrong.  MR_NEW() allocates memory with MR_malloc(),
which is the same as malloc except that it checks whether the
return value is NULL and if so calls MR_fatal_error().

Perhaps you meant MR_GC_NEW()?  MR_GC_NEW() calls MR_GC_malloc(),
which calls either GC_malloc() or malloc() depending on
whether conservative GC is enabled.  But using that has
two drawbacks compared to using MR_make_aligned_string_copy().
Firstly, when using conservative GC,
MR_make_aligned_string_copy() uses GC_malloc_atomic()
rather than GC_malloc(), which eliminates the possibility of accidental
memory retention due to chance bit patterns in the string.
Secondly, when not using conservative GC, MR_make_aligned_string_copy()
allocates on the Mercury heap, where the memory can be reclaimed on
backtracking or by the type-accurate GC, whereas MR_GC_NEW() allocates
with malloc(), which in these circumstances will result in a memory leak.

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
The University of Melbourne         |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.
--------------------------------------------------------------------------
mercury-users mailing list
post:  mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-users-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the users mailing list