Remove (*Config).Close()
This commit is contained in:
@@ -64,11 +64,6 @@ func main() {
|
||||
// be set once the device is opened. All configuration references must be closed,
|
||||
// to free up the memory in libusb.
|
||||
cfgs, err := dev.Configurations()
|
||||
defer func() {
|
||||
for _, cfg := range cfgs {
|
||||
cfg.Close()
|
||||
}
|
||||
}()
|
||||
if err != nil {
|
||||
log.Printf(" - configs: %s", err)
|
||||
continue
|
||||
|
@@ -7,7 +7,6 @@ import "C"
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"runtime"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
@@ -53,8 +52,6 @@ func (i InterfaceInfo) String() string {
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
cfg *C.struct_libusb_config_descriptor
|
||||
|
||||
Type DescriptorType
|
||||
Config uint8
|
||||
Attributes uint8
|
||||
@@ -68,7 +65,6 @@ func (c Config) String() string {
|
||||
|
||||
func newConfig(cfg *C.struct_libusb_config_descriptor) *Config {
|
||||
c := &Config{
|
||||
cfg: cfg,
|
||||
Type: DescriptorType(cfg.bDescriptorType),
|
||||
Config: uint8(cfg.bConfigurationValue),
|
||||
Attributes: uint8(cfg.bmAttributes),
|
||||
@@ -121,24 +117,5 @@ func newConfig(cfg *C.struct_libusb_config_descriptor) *Config {
|
||||
}
|
||||
c.Interfaces = append(c.Interfaces, descs)
|
||||
}
|
||||
|
||||
// *sigh*
|
||||
runtime.SetFinalizer(c, (*Config).Close)
|
||||
|
||||
//log.Printf("config %p initialized", c.cfg)
|
||||
return c
|
||||
}
|
||||
|
||||
// Close decrements the reference count for the device in the libusb driver
|
||||
// code. It should be called exactly once!
|
||||
//
|
||||
// TODO(kevlar): This information can probably be cached at creation time
|
||||
// and then immediately closed.
|
||||
func (c *Config) Close() error {
|
||||
if c.cfg != nil {
|
||||
//log.Printf("config %p closed", c.cfg)
|
||||
C.libusb_free_config_descriptor(c.cfg)
|
||||
}
|
||||
c.cfg = nil
|
||||
return nil
|
||||
}
|
||||
|
@@ -37,27 +37,20 @@ func newDescriptor(dev *C.libusb_device) (*Descriptor, error) {
|
||||
|
||||
// Configurations returns a list of configurations configured on this
|
||||
// device. Each config must be Closed.
|
||||
func (d *DeviceInfo) Configurations() (_c []*Config, _e error) {
|
||||
func (d *DeviceInfo) Configurations() ([]*Config, error) {
|
||||
var desc C.struct_libusb_device_descriptor
|
||||
if errno := C.libusb_get_device_descriptor(d.dev, &desc); errno < 0 {
|
||||
return nil, usbError(errno)
|
||||
}
|
||||
|
||||
var cfgs []*Config
|
||||
defer func() {
|
||||
if _e != nil {
|
||||
for _, c := range cfgs {
|
||||
c.Close()
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
for i := 0; i < int(desc.bNumConfigurations); i++ {
|
||||
var cfg *C.struct_libusb_config_descriptor
|
||||
if errno := C.libusb_get_config_descriptor(d.dev, C.uint8_t(i), &cfg); errno < 0 {
|
||||
return nil, usbError(errno)
|
||||
}
|
||||
cfgs = append(cfgs, newConfig(cfg))
|
||||
C.libusb_free_config_descriptor(cfg)
|
||||
}
|
||||
return cfgs, nil
|
||||
}
|
||||
|
@@ -49,11 +49,6 @@ func TestEnum(t *testing.T) {
|
||||
t.Logf("- Protocol: %s", usbid.Classify(desc))
|
||||
|
||||
cfgs, err := dev.Configurations()
|
||||
defer func() {
|
||||
for _, cfg := range cfgs {
|
||||
cfg.Close()
|
||||
}
|
||||
}()
|
||||
if err != nil {
|
||||
t.Errorf(" - configs: %s", err)
|
||||
continue
|
||||
|
Reference in New Issue
Block a user