Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unhandled memory fault + Error calling finalizer #22

Open
Le-Brut opened this issue Jul 16, 2020 · 3 comments
Open

Unhandled memory fault + Error calling finalizer #22

Le-Brut opened this issue Jul 16, 2020 · 3 comments

Comments

@Le-Brut
Copy link

Le-Brut commented Jul 16, 2020

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?

@iamgreaser
Copy link

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.

@Zulu-Inuoe
Copy link

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.

@izz-j
Copy link

izz-j commented Jun 19, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants