What are the consequences of not calling libusb_exit ()

I am writing a user space program that interacts with a USB video playback controller. I program in C ++, and the program is designed to work on Linux. Studying the libusb manual, I came across the void libusb_exit ( struct libusb_context * ctx ) function void libusb_exit ( struct libusb_context * ctx ) .

Description says :

Deinitialize libusb.

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

The manual does not explain why this is necessary. I became interested in the consequences of terminating a program that initialized and used libusb without calling libusb_exit() . Can someone explain what might happen if, for some reason, my program cannot call libusb_exit() before terminating? Will there be a leak in system resources?

+7
c ++ linux c ++ 14 libusb
source share
1 answer

This is something related to contexts.

Since you have one user application, you usually use the default context. It dies whenever a user session is destroyed, probably when your application should be closed.
Also note that you cannot leak simply because you do not call libusb_exit if your application crashes (well, although there may be a leak, the leaked memory will be released immediately after the failure, so I would not care more than about the cause of the accident itself )

The problem occurs when you have several sessions.
See here and here for more details.
If you do not call libusb_exit in this case, and the session is in the released state from the point of view of your application, you are of course about to leak memory because the context will not actually be destroyed by libusb. In fact, in this case, the software should not be shut down, but this memory is still in use and is no longer available since you did not call libusb_exit to release it.

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

+3
source share

All Articles