Lisp HUG Maillist Archive

Window Procedure memory management (MSWIN-specific)

Suppose I have some subclassed window procedure written in Lisp as a foreign-
callable:

(fli:define-foreign-callable
    (lisp-wnd-proc :result-type (:one-of :long (:boolean :long))
	           :calling-convention :stdcall)
    ((hWnd HWND) (uMsg ULONG) (wParam ULONG) (lParam ULONG))
  (case uMsg
     ;; Handle messages here...
      )
  (prog1
    ; Pass control back to the original window procedure...
    (CallWindowProc *orig-wnd-proc* hWnd uMsg wParam lParam)
      (when (= uMsg WM_NCDESTROY)
	; Some Lisp clean-ups here... 
        ))))

It might be that we receive, via lParam, the address of some struct. Now how 
exactly should this be handled?

Obviously one can't coerce/cast a raw memory address, so what I have been doing 
is simply to create an FLI pointer of the struct type in question and set its 
address to the address passed through lParam. But then what do I do with this 
pointer when it is no longer needed? Given that the memory for the struct is 
handled by the OS my assumption has been that I should not have to explicitly 
free the pointer.

This is what Section 3.1.3 of the FLI Guide says:

'If a foreign object is no longer needed, it should be deallocated using
free-foreign-object. This should be done only once for each foreign object, 
regardless of the number of pointer objects that contain its address.'

So if the OS is freeing the memory allocated for the struct by my reckoning the 
above implies that I don't have to explicitly free the pointer I created.

So am I doing the right thing? Or is this where the :reference keyword and its 
friends come into play?

Thanks in advance,
Chris


Updated at: 2020-12-10 08:37 UTC