On Sunday, January 18, 2015 at 10:32:30 AM UTC-5, Kenny McCormack wrote:
> The question is: What exactly does "make_const_string" do? In particular,
> does it do "garbage collection" - i.e., does it take care of free'ing
> strings when they are no longer in use? Otherwise, it seems to me that if
> you use this function repeatedly (as one will do), you will create a memory
> leak.
The make_const_string function is no more complicated than its description in section 16.4.5:
`static inline awk_value_t *'
`make_const_string(const char *string, size_t length, awk_value_t *result)'
This function creates a string value in the `awk_value_t' variable
pointed to by `result'. It expects `string' to be a C string
constant (or other string data), and automatically creates a
_copy_ of the data for storage in `result'. It returns `result'.
It has no magical garbage collection powers.
I agree that the memory management scheme may be a bit confusing. Please take a look at section 16.4.1 (the API introduction) where it says:
* All pointers filled in by `gawk' are to memory managed by `gawk'
and should be treated by the extension as read-only. Memory for
_all_ strings passed into `gawk' from the extension _must_ come
from calling the API-provided function pointers `api_malloc()',
`api_calloc()' or `api_realloc()', and is managed by `gawk' from
then on.
So there's a clear paradigm specified here for how memory will be managed. If you pass allocated memory into gawk, gawk will take control of it from them on. Perhaps this is not sufficiently emphasized in the documentation, but it is stated. Actually, now that I look, the newest (unreleased) version of the documentation has an "Extension summary" section that also includes this language:
* _All_ memory passed from `gawk' to an extension must be treated as
read-only by the extension.
* _All_ memory passed from an extension to `gawk' must come from the
API's memory allocation functions. `gawk' takes responsibility for
the memory and releases it when appropriate.
So help is on the way. Arnold has made a number of recent improvements to the documentation.
If you're curious about whether there are any memory leaks and happen to be using linux, valgrind can be very helpful for catching them. I sometimes use that to confirm that I haven't made any mistakes.
Regards,
Andy