Navigation:  Programming Cookbook > External Interfacing > External Libraries >

Caution: Objects may Move or Disappear

Previous pageReturn to chapter overviewNext page

If one passes a Smalltalk object to an external library function by reference (i.e. as an lpvoid or lpstr parameter) and the function is likely to capture that parameter (i.e. save it down and write back into it after the call has returned), then one must ensure that the object is allocated from fixed memory (ExternalStructures are automatically allocated in this way using the fixed allocation primitive). If one does not do this, the object may move in memory during garbage collection, and unpredictable behaviour will be the result!

One must also ensure that externally captured objects continue to be referenced from Smalltalk in order that they are not garbage collected, or the result will be the same. This is particularly likely to be an issue when pointers to objects are stored into the fields of ExternalStructures. The idiom to use in this case is to add an instance variable that can be set to point to the object at the same time as its address is stored down. In this way a reference that the garbage collector can see is maintained for the life of the structure, and so the referenced object will not disappear while the structure is still in use.