This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

EXCEPTION_ACCESS_VIOLATION???

4 posts · 1733 views

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:
(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:
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:
(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:
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;
};

Re: EXCEPTION_ACCESS_VIOLATION???

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:
(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)

Re: EXCEPTION_ACCESS_VIOLATION???

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

Re: EXCEPTION_ACCESS_VIOLATION???

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.