Hi David, David Pirotte <da...@altosw.be> writes:
>> 'make-c-struct' copies the C pointers from those foreign pointer objects, >> but not >> not keep a reference to the objects themselves. > > To me, this sounds very counter intuitive, actually, it sounds like a bug, > make-c-struct should be holding a reference to the pointers it receives: i > seems to > me that only when the c-struct itself becomes unreachable, that these > pointers could > be freed? It's a reasonable suggestion, and I agree that the documentation should be clarified on these points, which are quite tricky. I thought about what would be required to implement your suggestion and quickly ran into difficulties, and also the understanding that this is not always the right behavior. First of all, the address of the foreign pointer object is what we would need to keep a reference to, and that's not the same as the C pointer wrapped within that foreign pointer object. This raises the question of where to store these additional references, since we can't store them in any of the C struct fields defined by the user. The worse problem is that there's no way to keep these extra references in sync with the C pointers in the C struct. The C code might overwrite one of those pointers, and move them to a different structure somewhere else. Also note that in many cases, foreign pointer objects are used only transiently to copy a C pointer from one place to another. If we implemented your suggestion, these otherwise transient foreign pointer objects would be kept alive for no purpose. The other thing is that even if we had these suggested semantics, you would still need to arrange to hold a reference to the foreign pointer object returned by 'make-c-struct'. It seems to me that the hard part is deciding when these references can be released. Once you've made that determination, releasing N references is almost as easy as releasing 1 reference. Let me ask you this: in your current use case, what _should_ be the policy on when to free those C strings? What will become of that C structure allocated with 'make-c-struct', and how will you find out when the C code no longer needs it? Regards, Mark