I'm on Windows 7 64bit, with MinGW/msys compiled ECL 12.7.1 (gc-7.2 (stable), libffi-3.0.11, gmp-5.0.5). ECL and all libs are compiled for 32bit (no problems compiling). My MinGW GCC version is 4.7.0.
I'm getting segfaults when calling out to CFFI, but only in some cases. It only happens when the code is compiled, bytecodes works fine:
(ql:quickload :cffi) (defpackage :tess-test (:use :cl :cffi)) (in-package :tess-test) (define-foreign-library glu (:windows "glu32.dll") ((:and :unix (:not :darwin)) (:or "libGLU.so.1" "libGLU.so")) ((:not :darwin) (:default "libGLU"))) (use-foreign-library glu) (defconstant +tess-winding-rule+ 100140) (defconstant +tess-winding-odd+ 100130) (defconstant +tess-end+ 100102) (defcallback tess-end-cb :void () nil) (declaim (optimize (speed 0) (debug 3) (safety 3))) (defun test () (let ((tess (foreign-funcall "gluNewTess" :pointer))) (foreign-funcall "gluTessProperty" :pointer tess :int +tess-winding-rule+ :double +tess-winding-odd+ :void) (format t "...set winding~%") (foreign-funcall "gluTessCallback" :pointer tess :int +tess-end+ :pointer (callback tess-end-cb)) (format t "...set cb~%") (foreign-funcall "gluDeleteTess" :pointer tess :void) (format t "...deleted tess~%")))
To run, I do
> (ext:install-c-compiler) > (load "tess-test") > (test) ...set winding ...set cb ...deleted tess > (compile 'test) ;;; OPTIMIZE levels: Safety=3, Space=0, Speed=0, Debug=3 ;;; ;;; End of Pass 1. TEST NIL NIL > (test) ...set winding Condition of type: SEGMENTATION-VIOLATION Detected access to an invalid or protected memory address.
I also tried doing (foreign-funcall ("gluTessCallback" :convention :stdcall) ...) but no luck.
I've tried attaching msys's GDB to the process, and it can detect the segfault, but doesn't allow me to backtrace it or get any pertinent information:
Program received signal SIGSEGV, Segmentation fault. [Switching to Thread 2676.0xa6c] 0x02e76732 in ?? () (gdb) bt #0 0x02e76732 in ?? () Cannot access memory at address 0x61c17
I've verified via procmon the the C compiler being used is c:/MinGW/bin/gcc.exe (the GCC I compiled everything with). I've tried playing with the C::cc-flags a bit but no luck (any suggestions here would be great).
ECL can compile (via MinGW), load, and run just about all code I throw at it, and I can load and run functions in some DLLs just fine (I do also have problems with GLFW, but I'm using GLU as an example because it's more widely available by default and I'm hoping the problems are related). I've tried messing with the c::* compiler/link flags but still the same problem. I've been at this for about two weeks now and figured it's about time to ask for help.
It may also be worth mentioning that the code is from the library glu-tessellate (https://github.com/orthecreedence/glu-tessellate) and works fine on my Clozure CL (32bit) implementation.
Thank you very much for reviewing this. I appreciate all the work that has gone into this project. Please let me know if there are any more details I can get you.