memory-managementcommon-lispforeignobject

Memory error caused by mismanaged C foreign objects in Common Lisp


Error

In the REPL, when re-opening the app:

Error: Received signal number 11 (Segmentation violation) [condition type: SYNCHRONOUS-OPERATING-SYSTEM-SIGNAL]

Also asks me to check point of error, DRAW-STRING-BLENDED-* from lispbuilder-sdl

When attempting to launch the app (it crashes on launch):

Unhandled memory fault at #x2968800.

Background

I use the lispbuilder-sdl library to interface with SDL. This library has a convenience macro WITH-INIT which handles the opening and closing of SDL and subsystems like audio and ttf fonts.

My attempts to resolve

1) I figured the memory from the font objects was not being properly freed. I store the font objects like this:

(defvar *menu-font* (initialise-font *ttf-font-simonetta-regular-50*))

I added lisp code to free all my pre-defined fonts as part of the shutdown process, but it didn't help.

2) I have a process in a separate thread which looks like this:

(defun playloop ()
  (setf *playthread* 
    (bt:make-thread
     ; executes lambda instantly upon call to setf
     (lambda ()
       (mapcar #'(lambda (pair)
                   (clear-display *black*)
                   (draw-lyric (car pair))
                   (update-display) 
                   (sleep (cdr pair)))
         *song*)))))

As part of the quit process, I have that thread destroyed. There's no problem quitting the app, it's just when re-starting that I get the error.

Any suggestions would be greatly appreciated.


Solution

  • Appears fixed now (able to start and re-start several times without error).

    I made a rookie mistake of initialising the fonts outside the body of the WITH-INIT macro mentioned in the description.

    I hope this will be useful to anyone using lispbuilder-sdl and struggling with C foreign objects.