GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Examples: Fix device leak in hotplugtest · Raghuvardhan-Reddy78/libusb@3037616

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • examples
    • hotplugtest.c
  • libusb
    • version_nano.h
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎examples/hotplugtest.c‎

Copy file name to clipboardExpand all lines: examples/hotplugtest.c
+21-3Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#include "libusb.h"
2525

2626
int done = 0;
27-
libusb_device_handle *handle;
27+
libusb_device_handle *handle = NULL;
2828

2929
static int LIBUSB_CALL hotplug_callback(libusb_context *ctx, libusb_device *dev, libusb_hotplug_event event, void *user_data)
3030
{
@@ -38,7 +38,15 @@ static int LIBUSB_CALL hotplug_callback(libusb_context *ctx, libusb_device *dev,
3838

3939
printf ("Device attached: %04x:%04x\n", desc.idVendor, desc.idProduct);
4040

41-
libusb_open (dev, &handle);
41+
if (handle) {
42+
libusb_close (handle);
43+
handle = NULL;
44+
}
45+
46+
rc = libusb_open (dev, &handle);
47+
if (LIBUSB_SUCCESS != rc) {
48+
fprintf (stderr, "Error opening device\n");
49+
}
4250

4351
done++;
4452

@@ -49,9 +57,13 @@ static int LIBUSB_CALL hotplug_callback_detach(libusb_context *ctx, libusb_devic
4957
{
5058
printf ("Device detached\n");
5159

52-
libusb_close (handle);
60+
if (handle) {
61+
libusb_close (handle);
62+
handle = NULL;
63+
}
5364

5465
done++;
66+
5567
return 0;
5668
}
5769

@@ -100,5 +112,11 @@ int main(int argc, char *argv[])
100112
printf("libusb_handle_events() failed: %s\n", libusb_error_name(rc));
101113
}
102114

115+
if (handle) {
116+
libusb_close (handle);
117+
}
118+
103119
libusb_exit (NULL);
120+
121+
return EXIT_SUCCESS;
104122
}
Collapse file

‎libusb/version_nano.h‎

Copy file name to clipboard
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
#define LIBUSB_NANO 10947
1+
#define LIBUSB_NANO 10948

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.