[squeak-dev] [FFI] ExternalStructure ownership (or not) of memory

John M McIntosh johnmci at smalltalkconsulting.com
Sat Apr 3 00:51:21 UTC 2010

In dealing with the case where you have a Smalltalk object that is paired to a external object where you want to ensure both are freed (at some point).  There are two choices

(a) You store a reference (somehow) to the external object in your smalltalk object. This can be some sort of index number that the external logic understands how to turn that into a reference to the memory, or you store the bytes for a memory pointer in the smalltalk instance.  Don't forget issues with 32 or 64 bit memory addresses.   You then always send your smalltalk object a release method that ensures it's external memory usage is cleaned up, either by using the memory pointer bytes or the index handle, when you decide to dispose of the smalltalk object. The release method then talks to the external interface to dispose of the external memory. 

(b) Like above, but you use Finalization to ensure external memory gets cleaned up when the Smalltalk object goes away. 

On 2010-04-02, at 5:17 PM, Schwab,Wilhelm K wrote:

> Hello all,
> Suppose I have a pointer to a structure and I do or do not have the responsibility of reclaiming the memory when a corresponding ExternalStructure sub-instance is finalized.  How do I distinguish the two cases?  One idea I had is to explicitly set the handle to nil if I do not own the memory, but (please correct me if I'm wrong), I can't do that while I'm still using the instance because it will presumably use the handle in accessing the fields, right?
> Looking at implementors of #isExternalAddress, maybe the idea is to use a byte array to make a copy of memory and an address to refer to it.
> What should I be asking? :)
> Bill

John M. McIntosh <johnmci at smalltalkconsulting.com>   Twitter:  squeaker68882
Corporate Smalltalk Consulting Ltd.  http://www.smalltalkconsulting.com

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 2445 bytes
Desc: not available
Url : http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20100402/4427cd69/smime.bin

More information about the Squeak-dev mailing list