[mercury-users] Memory management when CPP calls Mercury

Fergus Henderson fjh at cs.mu.OZ.AU
Fri Dec 14 00:18:31 AEDT 2001


On 13-Dec-2001, Ondrej Bojar <oboj7042 at ss1000.ms.mff.cuni.cz> wrote:
> I intend to use my Mercury predicates in a C++ program. My predicates get
> a string and return MR_list. I know, how to parse MR_list in C++, but I
> don't understand what exactly does conservative garbage collector.

With the conservative garbage collector,
the process of actually doing a garbage collection
happens as part of a call to the allocation routine
(e.g. GC_malloc()) when sufficient memory has been
allocated since the last full collection.

> I'm afraid of not releasing memory occupied by the MR_list when done in C.
> Proper releasing of memory is crucial for me, since the C++ program looks
> like:
> 
> while (1) {
>   char * a_new_thing;
>   MR_list parsed_thing;
> 
>   a_new_thing = malloc_and_get_the_thing_from_somewhere();
> 
>   parsed_thing = my_mercury_function(a_new_thing);
> 
>   read_mr_list_and_dump_it_somewhere(parsed_thing);
> 
>   free(a_new_thing);
> 
>   /* should I release MR_list parsed_thing now? And how? */
> }
>
> I release the string with free(a_new_thing), but how do I release MR_list
> parsed_thing? Or does this Mercury for me? When?

With the conservative collector, there is no need to
deallocate the list; it will get garbage collected
in some future iteration of the loop, when my_mercury_function()
does further memory allocation.  my_mercury_function() will allocate
memory by calling GC_malloc(), and GC_malloc() will eventually decides
that it is time to do garbage collection.

> I need also the other way:
> 
> while (1) {
>   int *input_array;
>   MR_list input_list;
>   char * parsed_thing;
> 
>   input_array = malloc_and_get_from_somewhere_a_list_of_numbers();
> 
>   input_list = use_MR_cons_to_build_MR_list_from_array(input_array);
> 
>   free(input_array);
> 
>   parsed_thing = my_mercury_function_to_parse_list(input_list);
> 
>   printf("Got: %s\n", parsed_thing);
> 
>   /* should I free parsed_thing now? (using free() I guess) */
> }

The same applies -- you don't need to free it.

Using free() will definitely not work.  If you really want to explicitly
deallocate the memory, you'd need to use GC_free() rather than free().
However, this only works if the memory was allocated on the conservative
garbage collector's heap (with GC_malloc()).  It will crash if, for
example, the string returned is one that the Mercury compiler has
allocated in read-only memory.  So I strongly advise against this.
Just leave it to the garbage collector.

-- 
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