Re: FLI and :REFERENCE types
Hi Frank,
On Mon, 24 Mar 2008 13:43:32 +0100, Frank Goenninger <frgo@mac.com> wrote:
> even if the memory is freed upon garbage collection it would be an
> error to do so.
>
> Imagine the situation if the C data structure you get returned via a
> pointer itself contains further data structures that have been
> allocated by malloc() and its friends... In Lisp land you wouldn't
> know about these and therefore the Lisp shouldn't free automatically
> any object without calling a deep-free function that recursively
> walks down the data structure and frees any memory having been
> allocated dynamically.
>
> When looking at your example with ULONG * there might be argued that
> this is a trivial case because the returned object is known not to
> contain any further dynamically allocated memory - or is the
> :unsigned-long meant to be a pointer address ? If not then
> deallocating the object would be ok - no memory leaks resulting.
>
> Not really the answer you were looking for but when Lispworks'
> garbage collector is programmed properly it would know that it can't
> free a NULL address. So a safe way to handle it would be:
>
> Free the data yourself.
> Set the address of the pointer to NULL.
> Done.
>
> I'd like to hear comments - I do so myself in another Lisp and there
> this works. And I have to port to LW at some point in time ...
I understand your concerns, but I think you're expecting too much.
We're talking about C, after all... :)
I would argue that a) if you declare something to be of type
:UNSIGNED-LONG and use it as a pointer you should know what you're
doing and b) if you free the space a C struct occupies which itself
points to other objects, that's not a memory leak. In the case of b),
another pointer could point to the same object, so your "deep-free"
function would be wrong unless it was a full GC with all bells and
whistles - and even then it would be wrong as it wouldn't be able to
know what happens in C land.
In other words, for things like this, there should be a clear
contract. If a C function asks you to allocate space for a struct and
then puts a pointer to some other object into this struct, it should
be clearly specified who is responsible for the memory occupied by
this object. This has nothing to do with the memory for the struct
itself, I'd say.
Coming back to my original question, I'd like to see the :REFERENCE
type as a kind of "translator" between C and Lisp. Once it has done
its work (in the case of :REFERENCE-RETURN), what I get back is a Lisp
object (which might "wrap" a C struct) that can be treated like any
other Lisp object - specifically, I can forget about it when it goes
out of scope. And I'm asking for confirmation that this POV is
correct... :)
Cheers,
Edi.