diff --git a/fakelibusb_test.go b/fakelibusb_test.go index b9ca735..cc645bb 100644 --- a/fakelibusb_test.go +++ b/fakelibusb_test.go @@ -126,12 +126,35 @@ func (f *fakeLibusb) getDevice(handle *libusbDevHandle) *libusbDevice { return f.handles[handle] } -func (f *fakeLibusb) exit(*libusbContext) error { +func (f *fakeLibusb) exit(ctx *libusbContext) error { close(f.submitted) - if got := len(f.ts); got > 0 { - for t := range f.ts { - f.free(t) - } + f.mu.Lock() + for d := range f.devices { + freeDevicePointer(d) + delete(f.devices, d) + } + var remainingHandles []*libusbDevHandle + for h := range f.handles { + remainingHandles = append(remainingHandles, h) + } + var remainingTransfers []*libusbTransfer + for t := range f.ts { + remainingTransfers = append(remainingTransfers, t) + } + f.mu.Unlock() + + freeContextPointer(ctx) + + for _, h := range remainingHandles { + f.close(h) + } + for _, t := range remainingTransfers { + f.free(t) + } + if got := len(remainingHandles); got > 0 { + return fmt.Errorf("fakeLibusb has %d remaining handles that should have been closed", got) + } + if got := len(remainingTransfers); got > 0 { return fmt.Errorf("fakeLibusb has %d remaining transfers that should have been freed", got) } return nil @@ -156,7 +179,10 @@ func (f *fakeLibusb) open(d *libusbDevice) (*libusbDevHandle, error) { func (f *fakeLibusb) close(h *libusbDevHandle) { f.mu.Lock() defer f.mu.Unlock() - delete(f.handles, h) + if _, ok := f.handles[h]; ok { + delete(f.handles, h) + freeDevHandlePointer(h) + } } func (f *fakeLibusb) reset(*libusbDevHandle) error { return nil } func (f *fakeLibusb) control(*libusbDevHandle, time.Duration, uint8, uint8, uint16, uint16, []byte) (int, error) { @@ -274,7 +300,10 @@ func (f *fakeLibusb) data(t *libusbTransfer) (int, TransferStatus) { func (f *fakeLibusb) free(t *libusbTransfer) { f.mu.Lock() defer f.mu.Unlock() - delete(f.ts, t) + if _, ok := f.ts[t]; ok { + delete(f.ts, t) + freeFakeTransferPointer(t) + } } func (f *fakeLibusb) setIsoPacketLengths(t *libusbTransfer, length uint32) { f.mu.Lock() diff --git a/libusb.go b/libusb.go index 0997dc8..b36eb8b 100644 --- a/libusb.go +++ b/libusb.go @@ -25,6 +25,7 @@ import ( /* #cgo pkg-config: libusb-1.0 +#include #include int gousb_compact_iso_data(struct libusb_transfer *xfer, unsigned char *status); @@ -550,7 +551,10 @@ func libusbSetDebug(c *libusbContext, lvl int) { C.gousb_set_debug((*C.libusb_context)(c), C.int(lvl)) } -// for obtaining unique CGo pointers. +// In tests, the code allocates some C pointers directly to obtain unique CGo +// pointers, and it needs to be able to free them directly too. These helpers +// are defined here because Go does not support cgo (import "C") in *_test.go +// files. func newDevicePointer() *libusbDevice { return (*libusbDevice)(unsafe.Pointer(C.malloc(1))) } @@ -566,3 +570,21 @@ func newContextPointer() *libusbContext { func newDevHandlePointer() *libusbDevHandle { return (*libusbDevHandle)(unsafe.Pointer(C.malloc(1))) } + +// In tests, the code allocates some C pointers directly, so it needs to be +// able to free them directly too. +func freeDevicePointer(d *libusbDevice) { + C.free(unsafe.Pointer(d)) +} + +func freeFakeTransferPointer(t *libusbTransfer) { + C.free(unsafe.Pointer(t)) +} + +func freeContextPointer(c *libusbContext) { + C.free(unsafe.Pointer(c)) +} + +func freeDevHandlePointer(h *libusbDevHandle) { + C.free(unsafe.Pointer(h)) +}