libusb icon indicating copy to clipboard operation
libusb copied to clipboard

libusb with go and msys compilation error

Open kernelPanic0x opened this issue 3 years ago • 1 comments

Hello,

Im trying to work with libusb and go. I have msys mingw64 and I've installed mingw-w64-x86_64-libusb-1.0.26-1 there. Pkg-Config finds it but when I compile the example:

// Copyright (c) 2016 The libusb developers. All rights reserved.
// Project site: https://github.com/gotmc/libusb
// Use of this source code is governed by a MIT-style license that
// can be found in the LICENSE.txt file for the project.

package main

import (
	"log"

	"github.com/gotmc/libusb"
)

func main() {
	ctx, _ := libusb.NewContext()
	defer ctx.Close()
	devices, _ := ctx.GetDeviceList()
	for _, device := range devices {
		usbDeviceDescriptor, _ := device.GetDeviceDescriptor()
		handle, _ := device.Open()
		defer handle.Close()
		serialNumber, _ := handle.GetStringDescriptorASCII(usbDeviceDescriptor.SerialNumberIndex)
		log.Printf("Found S/N: %s", serialNumber)
	}

}

Then i get this:

panic: runtime error: invalid memory address or nil pointer dereference
        panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x0 pc=0x7ff76d865a54]

goroutine 1 [running]:
github.com/gotmc/libusb.(*DeviceHandle).Close.func1(0x7ff76d8089f6?)
        C:/Users/elias/go/pkg/mod/github.com/gotmc/[email protected]/handle.go:65 +0x14
github.com/gotmc/libusb.(*DeviceHandle).Close(0x0?)
        C:/Users/elias/go/pkg/mod/github.com/gotmc/[email protected]/handle.go:65 +0x19
panic({0x7ff76d891ca0, 0x7ff76d87b700})
        C:/Program Files/Go/src/runtime/panic.go:890 +0x262
github.com/gotmc/libusb.(*DeviceHandle).GetStringDescriptorASCII.func1(0x7ff76d93b4e0?, 0x80?, {0xc000006600?, 0xc00011de98?, 0xc00011de98?}, 0x7ff76d7f531f?)
        C:/Users/elias/go/pkg/mod/github.com/gotmc/[email protected]/handle.go:51 +0x1d
github.com/gotmc/libusb.(*DeviceHandle).GetStringDescriptorASCII(0xc00006a480?, 0xf8?)
        C:/Users/elias/go/pkg/mod/github.com/gotmc/[email protected]/handle.go:56 +0x57
main.main()
        D:/Dokumente/Go Lang/project/cmd/main.go:22 +0xbd
exit status 2

Is the msys version incomatible? And I had to change some function names like device.DeviceDescriptor() -> device.GetDeviceDescriptor(). Has the API changed but the examples not?

kernelPanic0x avatar Aug 28 '22 15:08 kernelPanic0x

You should probably handle the errors being return by ctx.GetDeviceList(), device.GetDeviceDescriptoer(), and handle.GetStringDescriptorASCII.

matthewrankin avatar Aug 21 '23 20:08 matthewrankin