[Ecls-list] Foreign variables setting problem
Goffioul Michael
goffioul at imec.be
Mon Oct 24 04:37:29 UTC 2005
> On Mon, 2005-10-24 at 11:52 +0200, Goffioul Michael wrote:
> > What about si:free-foreign-data, which uses cl_dealloc().
> Is it correct
> > to use it on memory that has been malloc'ed? (At least when
> ECL use its
> > internal GBC, in the other case cl_dealloc is just empty).
>
> One should use free(). For that maybe one should add a flag in
> foreign_data, as in
>
> struct ecl_foreign { /* user defined datatype */
> HEADER1(malloced); /* allocated with malloc()? */
> cl_object tag; /* a tag identifying the type */
> cl_index size; /* the amount of memory allocated */
> char *data; /* the data itself */
> };
While trying to implement this, I just thought about a potential problem.
Imagine, your code do (with *var-string* being a foreign char* variable):
(setf *var-string* "hello")
(setf *var-string* "world")
On the first SETF, CFFI will allocate a foreign string and set *var-string*
to point to it. The second SETF will do the same, without deallocating the
string "hello". To deallocate it, you would have to create a foreign-data
object pointing to the same address as *var-string* and then free it, this
looks tricky....
Wouldn't it be easier to tell the GC to consider the foreign variable as a
potential pointer reference to avoid the memory pointed by it from being
collected?
Just a thought...
Michael.
More information about the ecl-devel
mailing list