cl-sdl2-ttf icon indicating copy to clipboard operation
cl-sdl2-ttf copied to clipboard

Unhandled memory fault + Error calling finalizer

Open Le-Brut opened this issue 4 years ago • 3 comments

Hi!

The following code runs fine for a few seconds (shows the text just fine)

(ql:quickload 'sdl2)
(ql:quickload 'sdl2-ttf)
(ql:quickload 'font-discovery)

(defmacro with-texture (texture-sym renderer surface &body body)
  `(let ((,texture-sym (sdl2:create-texture-from-surface ,renderer ,surface)))
     (unwind-protect
          (progn ,@body)
       (sdl2:destroy-texture ,texture-sym))))

(defmacro with-surface (surface-sym surface &body body)
  `(let ((,surface-sym ,surface))
     (unwind-protect
          (progn ,@body)
       (sdl2:free-surface ,surface-sym))))

(let ((font-path (org.shirakumo.font-discovery:file
                  (org.shirakumo.font-discovery:find-font :family "" :spacing :monospace)))
      (point-size 40))
  (sdl2:with-init (:everything)
    (sdl2-ttf:init)
    (sdl2:with-window (the-window :title "Basic Font Example" :w 300 :h 300 :flags '(:shown))
      (sdl2:with-renderer (my-renderer the-window :flags '(:accelerated))
        (let ((font (sdl2-ttf:open-font font-path point-size)))
          (sdl2:with-event-loop (:method :poll)
            (:idle ()
                   (sdl2:set-render-draw-color my-renderer 0 0 0 255)
                   (sdl2:render-clear my-renderer)
                   (with-surface surface (sdl2-ttf:render-text-solid font "tnetnet" 255 255 255 0)
                     (with-texture hello-text my-renderer surface
                       (sdl2:render-copy my-renderer
                                         hello-text
                                         :source-rect (cffi:null-pointer)
                                         :dest-rect (sdl2:make-rect
                                                     (round (- 150 (/ (sdl2:texture-width hello-text) 2.0)))
                                                     (round (- 150 (/ (sdl2:texture-height hello-text) 2.0)))
                                                     (sdl2:texture-width hello-text)
                                                     (sdl2:texture-height hello-text)))))
                   (sdl2:render-present my-renderer))
            (:quit ()
                   (when (> (sdl2-ttf:was-init) 0)
                     (sdl2-ttf:close-font font)
                     (sdl2-ttf:quit))
                   t)))))))

and then crashes with:

Unhandled memory fault at #x10014.
   [Condition of type SB-SYS:MEMORY-FAULT-ERROR]

and prints:

WARNING:
   Error calling finalizer #<CLOSURE (LAMBDA ()
                                       :IN
                                       SDL2-TTF:RENDER-TEXT-SOLID) {10064E1A2B}>:
  #<SB-SYS:MEMORY-FAULT-ERROR {100489D293}>

Checked on SBCL 2.0.3 and 2.0.5-1.

What could be the problem here? Is it CL-SDL2 bug?

Le-Brut avatar Jul 16 '20 11:07 Le-Brut

I have to chime in here, I am having this same issue on SBCL 2.0.0.

If I call sdl2:free-surface directly, I get the memory errors.

If I don't call sdl2:free-surface, I end up with more and more memory being used with no sign of the garbage collector kicking in.

I have a patch to remove the autocollects in render.lisp, but it may break the API as intended. If so, I'll stick to my own fork.

iamgreaser avatar Sep 07 '20 05:09 iamgreaser

In general it's not a great idea to be calling font freeing functions from a finalizer, since neither SDL2_TTF, nor SDL2 in general thread-safe, and finalizers can be invoked on any random thread.

Zulu-Inuoe avatar Dec 10 '20 00:12 Zulu-Inuoe

I am using SBCL version 2.1.5 and I still get the same issue as well. sdl2:free-surface is still the problem. It is not reliable. The same code works on my laptop running debian 10 but doesn't work on my desktop that is also running debian 10. Should bring the issue to cl-sdl2: https://github.com/lispgames/cl-sdl2/issues. As its not really an sdl2-ttf issue.

izz-j avatar Jun 19 '21 15:06 izz-j