In the previous example our defined interface function
used the function allocate-foreign-object to allocate memory for an instance of a
structure. This memory is now reserved, with a pointer to its location bound to the variable
. More detailed information on pointers is available in FLI Pointers. To free the memory associated with the foreign object requires the use of the function free-foreign-object.
There are other methods for dealing with the question of memory management. The following example defines a Lisp function that returns the x and y coordinates of the cursor without permanently tying up memory for structures that are only used once.
(defun current-cursor-position ()
(let ((lppoint (fli:allocate-dynamic-foreign-object
(if (get-cursor-position lppoint)
(values t (fli:foreign-slot-value lppoint 'x)
(fli:foreign-slot-value lppoint 'y))
(values nil 0 0)))))
the following happens:
get-cursor-positionis called with
GetCursorPoswas successful the function foreign-slot-value is called twice, once to return the value in the
xslot and again to return the value in the
yslot. If the call was unsuccessful then
0 0 nilis returned.