Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are consequences of not calling libusb_exit()

I am writing a userspace program which interacts with the USB video playback controller. I am programming in C++ and the program is intended to run on Linux. While studying the libusb manual I came across the void libusb_exit ( struct libusb_context * ctx ) function.

The description says:

Deinitialize libusb.

Should be called after closing all open devices and before your application terminates.

The manual does not explain why is it needed. I became curious about the consequences of terminating a program that had initialized and used libusb without calling libusb_exit(). Can someone explain what bad things could happen if for some reasons my program will fail to call libusb_exit() before terminating? Will it cause system resources to leak?

like image 694
VL-80 Avatar asked Apr 29 '16 19:04

VL-80


1 Answers

It is something that involves contexts.

As far as you have a single user application, you usually end up using the default context. That one dies whenever the user's session is destroyed, that is probably when your application is to be closed.
Note also that you cannot leak simply because you don't call libusb_exit if your app crashes (well, even though a leak is possible, the leaked memory is going to be released immediately after the crash, so I would not care about that more than about the reason of the crash itself).

The problem arises whenever you have multiple sessions.
See here and here for further details.
If you fail calling libusb_exit in such a case and the session is in a released state from the point of view of your application, you are certainly going to leak memory, for the context won't be actually destroyed by libusb. In fact, in this case the software is not to be closed, but that memory is still in use and no longer reachable, for you didn't invoked libusb_exit to release it.

That's why the documentation suggests to call libusb_exit every time you want to destroy a context, either the default one or not.

like image 96
skypjack Avatar answered Oct 03 '22 18:10

skypjack