What are consequences of not calling libusb

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?


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.

链接地址: http://www.djcxy.com/p/33708.html

上一篇: Swift扩展仅适用于导入它们的情况

下一篇: 什么是不调用libusb的后果