lispgames / cl-sdl2

Common Lisp bindings for SDL2 using C2FFI.
MIT License
304 stars 82 forks source link

Unhandled memory fault + Error calling finalizer #131

Closed Le-Brut closed 4 years ago

Le-Brut commented 4 years ago

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? Could it be a CL-SDL2-TTF bug (opened the issue here)?

mfiano commented 4 years ago

Yes, the problem is cl-sdl2-ttf hasn't been updated since cl-sdl2 was fixed to remove finalizers 2 years ago.

Le-Brut commented 4 years ago

@mfiano Got it, thanks for such a prompt response!