EXCEPTION_ACCESS_VIOLATION???

Discussion of Common Lisp
Post Reply
Harnon
Posts: 78
Joined: Wed Jul 30, 2008 9:59 am

EXCEPTION_ACCESS_VIOLATION???

Post by Harnon » Tue Sep 15, 2009 7:57 am

So I have tried over and over again to make some bindings to the libusb-win32 library. Every single time, when i attempt to access a certain structure returned, i get the EXCEPTION_ACCESS_VIOLATION. I'm on sbcl 1.0.29 on windows vista.
Plus, when i run the test C exe program, everything works out. But this wont... :cry:
Is there some obvious reason that its returning this, maybe i'm attempting to access something incorrectly???

Test Code:

Code: Select all

(usb_init) ;;no return value
(usb_find_busses) ;;returns 1
(usb_find_devices) ;;returns 5

(let ((bus (usb_get_busses)))
  (format t "BUS IS ~A~%" bus)
  (loop 
    for device = (cffi:foreign-slot-value bus 'usb_bus 'devices)
                 then (cffi:foreign-slot-value device 'usb_device 'next)
       until (cffi:null-pointer-p device)
   do(format t "DEVICE IS ~A~%" device)))
Output:

Code: Select all

BUS IS #.(SB-SYS:INT-SAP #X02131958)
DEVICE IS #.(SB-SYS:INT-SAP #X02131964)
DEVICE IS #.(SB-SYS:INT-SAP #X00000030)
EXCEPTION_ACCESS_VIOLATION
   [Condition of type SIMPLE-ERROR]
	0: [RETRY] Retry SLIME REPL evaluation request.
	1: [ABORT] Return to SLIME's top level.
	2: [ABORT] Abort
	3: [CLOSE-CONNECTION] Close SLIME connection
	4: [ABORT] Exit debugger, returning to top level.
Defs:

Code: Select all

(DEFCSTRUCT USB_BUS 
  (NEXT :POINTER) (PREV :POINTER) (DIRNAME :STRING)
  (DEVICES :POINTER) (LOCATION :UNSIGNED-LONG)
  (ROOT_DEV :POINTER))

(DEFCSTRUCT USB_DEVICE 
  (NEXT :POINTER) (PREV :POINTER) (FILENAME :STRING)
  (BUS :POINTER) (DESCRIPTOR :POINTER) (CONFIG :POINTER)
  (DEV :POINTER) (DEVNUM :UNSIGNED-CHAR)
  (NUM_CHILDREN :UNSIGNED-CHAR) (CHILDREN :POINTER))
C Structure Defs:

Code: Select all

struct usb_bus {
  struct usb_bus *next, *prev;

  char dirname[LIBUSB_PATH_MAX];

  struct usb_device *devices;
  unsigned long location;

  struct usb_device *root_dev;
};


struct usb_device {
  struct usb_device *next, *prev;

  char filename[LIBUSB_PATH_MAX];

  struct usb_bus *bus;

  struct usb_device_descriptor descriptor;
  struct usb_config_descriptor *config;

  void *dev;		/* Darwin support */

  unsigned char devnum;

  unsigned char num_children;
  struct usb_device **children;
};

dmitry_vk
Posts: 96
Joined: Sat Jun 28, 2008 8:01 am
Location: Russia, Kazan
Contact:

Re: EXCEPTION_ACCESS_VIOLATION???

Post by dmitry_vk » Tue Sep 15, 2009 8:15 am

Your defcstruct's are wrong. :string declares a structure field that is a pointer, but in your case it should inline array. Also, inline structures are not pointers. And because of this, 'devices' slot is taken from the wrong offset. Correct definition is like this:

Code: Select all

(defcstruct usb-bus
  (next :pointer)
  (prev :pointer)
  (dirname :char :pointer 512)
  (devices :pointer)
  (location :uint)
  (root-dev :pointer))

(defcstruct usb-device
  (next :pointer)
  (prev :pointer)
  (filename :char :count 512)
  (bus :pointer)
  (descriptor usb-device-descriptor) ;; there should be (defcstruct usb-device-descriptor ...)
  (config :pointer)
  (dev :pointer)
  (devnum :uchar)
  (num-children :uchar)
  (children :pointer))
(the 512 constant was taken from pylibusb.py for win32 libusb; it should be replaced for linux and mac os x)

Harnon
Posts: 78
Joined: Wed Jul 30, 2008 9:59 am

Re: EXCEPTION_ACCESS_VIOLATION???

Post by Harnon » Tue Sep 15, 2009 9:02 am

Yes! Thank you, it worked wonderfully. Good to know, didn't know this before.

Balooga
Posts: 14
Joined: Tue Jul 15, 2008 1:20 pm

Re: EXCEPTION_ACCESS_VIOLATION???

Post by Balooga » Tue Sep 15, 2009 11:40 am

dmitry_vk wrote:Your defcstruct's are wrong. :string declares a structure field that is a pointer, but in your case it should inline array.
I have been bitten by that before.

Post Reply