longlene / cl-raylib

Common Lisp binding of raylib
MIT License
145 stars 21 forks source link

Memory fault with struct passed by value #18

Closed aymanosman closed 2 years ago

aymanosman commented 2 years ago

Problem

sbcl-2.1.9-macosx-x64 (macOS Monteray)

I haven't been using this library for a long, but when I upgraded to Monteray I started getting this error:

;; cl-raylib-bug.lisp
(ql:quickload :cl-raylib)

(in-package #:raylib)

(init-audio-device)
(defvar boom (load-sound "boom.wav"))
(format t "Sound: ~a~%" boom)
(format t "type q to quit, otherwise play sound~%")
(loop :while (not (char= #\q (read-char)))
      :do (play-sound boom))
(sb-ext:quit)
$ sbcl --load cl-raylib-bug.lisp
INFO: AUDIO: Device initialized successfully
INFO:     > Backend:       miniaudio / Core Audio
INFO:     > Format:        32-bit IEEE Floating Point -> 32-bit IEEE Floating Point
INFO:     > Channels:      2 -> 2
INFO:     > Sample rate:   44100 -> 44100
INFO:     > Periods size:  1323
INFO: FILEIO: [boom.wav] File loaded successfully
INFO: WAVE: Data loaded successfully (48000 Hz, 16 bit, 2 channels)
INFO: WAVE: Unloaded wave data from RAM
Sound: (1291890176 (44100 32 2 #.(SB-SYS:INT-SAP #X0000E7F0)))
type q to quit, otherwise play sound
a
CORRUPTION WARNING in SBCL pid 58696 pthread 0x8d23600:
Memory fault at 0x6a817178 (pc=0x1288cb8, fp=0x65ef40, sp=0x65ef40) pthread 0x8d23600
The integrity of this image is possibly compromised.
Continuing with fingers crossed.
While evaluating the form starting at line 8, column 0
  of #P"/Users/ayman/git/github.com/aymanosman/common-lisp-study-group/apps/game/cl-raylib-bug.lisp":

debugger invoked on a SB-SYS:MEMORY-FAULT-ERROR in thread
#<THREAD "main thread" RUNNING {10015301C3}>:
  Unhandled memory fault at #x6A817178.

("bogus stack frame")

Solution

I made incomplete bindings using cffi-grovel to handle defining the C structs and it fixed the issue, leading me to think that it something to do with struct fields being wrongly defined in this library.

The bindings are here https://github.com/aymanosman/cl-raylib

Using them, I don't see the error anymore. The most obvious difference is the frameCount in your version of the library is wrong, it reports 1291890176 while the correct number is 59376.

Sound: (FRAME-COUNT 59376 STREAM
        (CHANNELS 2 SAMPLE-SIZE 32 SAMPLE-RATE 44100 BUFFER
         #.(SB-SYS:INT-SAP #X7FC69F011800)))
longlene commented 2 years ago

@aymanosman Sorry for the late reply, I'm a little busy these days. The root cause is that I haven't update the lisp code for the latest raylib. It should be easy to fix. I'll fix it ASAP.

longlene commented 2 years ago

@aymanosman Please try the latest master. I also port some examples from raylib.

aymanosman commented 2 years ago

I can confirm that it works, thanks!