pointers - Lisp, cffi, let and memory -
i've build toy c++ library create qt window lisp. know common-qt exists, i'm trying learn how use cffi.
right now, have 4 binded functions :
- create-application : create qapplication , return pointer
- create-window : create qmainwindow , return poiner
- show : show window specified argument
- exec : qt exec function
here lisp code work :
(defctype t-app :pointer) (defctype t-window :pointer) (defcfun (create-application "create_application" ) t-app) (defcfun (exec "exec") :void (app t-app)) (defcfun (create-window-aalt "create_window_aalt") t-window) (defcfun (show "show") :void (o t-window)) (defparameter (create-application)) (defparameter w (create-window-aalt)) (show w) (exec a)
but if use let or let*...i have memory fault !
(let* ((a (create-application)) (w (create-window-aalt))) (show w) (exec a)) corruption warning in sbcl pid 1312(tid 140737353860992): memory fault @ a556508 (pc=0x7ffff659b7f1, sp=0x7ffff2bbe688) integrity of image possibly compromised. exiting.
does know why ?
i using sbcl :
env ld_library_path=`pwd` \ env ld_preload=/usr/lib/libqtgui.so.4 \ sbcl --script aalt.lisp
thanks.
i suggest following:
since writing library c++ , using symbols lisp, make sure use
extern "c"
declarations - needed ensure c++ compiler not mangle names.check library works in c (not c++) application. ensure library working (e.g., not throw c++ exceptions).
upd:
i've tried run code , had same crash. problem seems in create_application
function. i've attached fixed version of function @ http://paste.lisp.org/display/138049.
concretely, there 2 problems:
create_application
allocatedv
on stack. subsequent code (i.e.,let
binding) overwrites it.argv
shouldnull
-terminated. i.e., should containargc+1
elements - last element null. (qt seems not use this, habit write code according specs).
in case, stack allocation problem - seems let
binding overwrites value of v
on stack, crashing sbcl. using malloc
or new
allocate argv
on heap fixes issue.
Comments
Post a Comment