1. Allocating string space
I have many routines in GraphApp.e which use allocate_string(), for example:
global function execapp(sequence cmd)
return c_func(execapp_,{allocate_string(cmd)})
end function --( in c: int execapp(char *cmd) )
Question:
Am I causing major memory leaks?
Does allocate_string allocate a whole new string with a c-style pointer, and if
so, does it free this string once the function is exited?
Would it be better to write this as:
global function execapp(sequence cmd)
atom s
s = allocate_string(cmd)
return c_func(execapp_,{s})
end function
Or like this:
global function execapp(sequence cmd)
atom s
object result
s = allocate_string(cmd)
result = c_func(execapp_,{s})
free(s)
return result
end function
Thanks,
Irv
2. Re: Allocating string space
Irv Mullins wrote:
>Does allocate_string allocate a whole new string
> with a c-style pointer, and if so, does it free this
> string once the function is exited?
You should free the string, as you described.
The good news is that the memory should be freed automatically when Euphoria
shuts down. So it's not really not that bad - at least for small
applications.
In Win32Lib, there is a allot_string/free_strings pair filched from Pete.
That way, I can allocate a bunch of strings as I need them, and then free
them all in a single statement.
-- David Cuny