Quantcast

"memory leak" debug message after libusb_close, and upon application exit.

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

"memory leak" debug message after libusb_close, and upon application exit.

John Clark
I've been debugging my Windows App using 1.0.8 the  libusb library.

Upon exit, using Visual Studio 2010 debug, the debug message window has a number of messages relating to a memory leak. From the diagnostic it appears to be memory associated with the list of USB devices that was 'scanned' and built during either the libusb_init, or libusb_open_device_with_vid_pid.

Is there some other house keeping function to be called when exiting the program, or is this just 'harmless' as the program is exiting anyway.

John Clark.


------------------------------------------------------------------------------
This SF email is sponsosred by:
Try Windows Azure free for 90 days Click Here
http://p.sf.net/sfu/sfd2d-msazure
_______________________________________________
Libusb-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/libusb-devel
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: "memory leak" debug message after libusb_close, and upon application exit.

Xiaofan Chen
On Wed, Mar 21, 2012 at 6:54 AM, John Clark <[hidden email]> wrote:
> I've been debugging my Windows App using 1.0.8 the
> libusb library.

Which version? Are you using libusb.git or the binary
snapshots of libusb-pbatard?

> Upon exit, using Visual Studio 2010 debug, the debug message
> window has a number of messages relating to a memory leak.
> From the diagnostic it appears to be memory associated with
> the list of USB devices that was 'scanned' and built during either
> the libusb_init, or libusb_open_device_with_vid_pid.
>
> Is there some other house keeping function to be called when
> exiting the program, or is this just 'harmless' as the program is
> exiting anyway.

What if you try libusb_exit() which is the counterpart of
libusb_init()?
http://libusb.sourceforge.net/api-1.0/group__lib.html

Or you can try the following functions.
libusb_release_interface()
libusb_free_device_list()
http://libusb.sourceforge.net/api-1.0/group__dev.html


--
Xiaofan

------------------------------------------------------------------------------
This SF email is sponsosred by:
Try Windows Azure free for 90 days Click Here
http://p.sf.net/sfu/sfd2d-msazure
_______________________________________________
Libusb-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/libusb-devel
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: "memory leak" debug message after libusb_close, and upon application exit.

Xiaofan Chen
In reply to this post by John Clark
On Wed, Mar 21, 2012 at 6:54 AM, John Clark <[hidden email]> wrote:

> I've been debugging my Windows App using 1.0.8 the
>  libusb library.
>
> Upon exit, using Visual Studio 2010 debug, the debug message
>  window has a number of messages relating to a memory leak.
> From the diagnostic it appears to be memory associated with the
>  list of USB devices that was 'scanned' and built during either
> the libusb_init, or libusb_open_device_with_vid_pid.
>
> Is there some other house keeping function to be called when
> exiting the program, or is this just 'harmless' as the program
>  is exiting anyway.

I got a reply to my inbox saying that it is a potential
memory leak bug in libusb.

Hopefully the expert here can check whether it is
really a problem of libusb or the application.

++++++++++++++
Further to the email to the libusbdev list that I have copy
pasted below, I had the same issue. I have found the
cause of the memory leak. It's in usb_get_paths().
 A pointer to the local stack variable ctx is being passed
 into libusb_init(). This pointer will of course never evaluate
 to 0, the value against which it is tested in libusb_init().
As a result, libusb_init will allocated a new libusb_context.
And this context will never get freed.

void usb_get_paths(
struct _icoms *p
) {
#ifdef ENABLE_USB
    ssize_t i, nlist;
    libusb_context *ctx = NULL;

    struct libusb_device **list;

    /* Scan the USB busses for instruments we recognise */
    /* We're not expecting any of our unstruments to be an interface
on a device. */

       libusb_init(&ctx);

    if (p->debug > 8)
        libusb_set_debug(ctx, p->debug);

    nlist = libusb_get_device_list(NULL, &list);

    if (p->debug) fprintf(stderr,"usb_get_paths about to look through
devices:\n");

    for (i = 0; i < nlist; i++) {
        usb_check_and_add(p, list[i]);
       }

    libusb_free_device_list(list,  1);
#endif /* ENABLE_USB */
}

+++++++++++++++++++++++++

--
Xiaofan

------------------------------------------------------------------------------
For Developers, A Lot Can Happen In A Second.
Boundary is the first to Know...and Tell You.
Monitor Your Applications in Ultra-Fine Resolution. Try it FREE!
http://p.sf.net/sfu/Boundary-d2dvs2
_______________________________________________
Libusb-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/libusb-devel
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: "memory leak" debug message after libusb_close, and upon application exit.

Peter Stuge
Xiaofan Chen wrote:
> On Wed, Mar 21, 2012 at 6:54 AM, John Clark <[hidden email]> wrote:
> > Upon exit, using Visual Studio 2010 debug, the debug message
> >  window has a number of messages relating to a memory leak.
>
> I got a reply to my inbox saying that it is a potential
> memory leak bug in libusb.
>
> Hopefully the expert here can check whether it is
> really a problem of libusb or the application.

It's a problem in the application.


> A pointer to the local stack variable ctx is being passed
> into libusb_init().

This is a problem.


> As a result, libusb_init will allocated a new libusb_context.
> And this context will never get freed.

And this is why the application ends up leaking.


> void usb_get_paths(
> struct _icoms *p
> ) {
> #ifdef ENABLE_USB
>     ssize_t i, nlist;
>     libusb_context *ctx = NULL;
>
>     struct libusb_device **list;
>
>     /* Scan the USB busses for instruments we recognise */
>     /* We're not expecting any of our unstruments to be an interface
> on a device. */
>
>        libusb_init(&ctx);

Big no-no. Every call to libusb_init() for a given context must have
a matching call to libusb_exit() for the same context. I'd appreciate
improvements to make this more clear in the documentation.

If there is no need in the application to use a specific context
(using a context is very important if writing a library that uses
libusb, but is not always needed when writing an application) then
pass NULL to libusb_init() and libusb_exit(), and then libusb will
use the default context.


//Peter

------------------------------------------------------------------------------
For Developers, A Lot Can Happen In A Second.
Boundary is the first to Know...and Tell You.
Monitor Your Applications in Ultra-Fine Resolution. Try it FREE!
http://p.sf.net/sfu/Boundary-d2dvs2
_______________________________________________
Libusb-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/libusb-devel
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: "memory leak" debug message after libusb_close, and upon application exit.

Xiaofan Chen
On Sat, Apr 14, 2012 at 9:21 AM, Peter Stuge <[hidden email]> wrote:
> Xiaofan Chen wrote:
>> I got a reply to my inbox saying that it is a potential
>> memory leak bug in libusb.
>>
>> Hopefully the expert here can check whether it is
>> really a problem of libusb or the application.
>
> It's a problem in the application.

Thanks for the explanations.

>> A pointer to the local stack variable ctx is being passed
>> into libusb_init().
>
> This is a problem.
>
>
>> As a result, libusb_init will allocated a new libusb_context.
>> And this context will never get freed.
>
> And this is why the application ends up leaking.
>
> Big no-no. Every call to libusb_init() for a given context must have
> a matching call to libusb_exit() for the same context. I'd appreciate
> improvements to make this more clear in the documentation.

It seems to me that the documentation is quite clear here.
http://libusb.sourceforge.net/api-1.0/group__lib.html

int libusb_init ( libusb_context ** context )
Initialize libusb.
This function must be called before calling any other libusb function.
Parameters:
context Optional output location for context pointer. Only valid on
return code 0.
Returns:
0 on success, or a LIBUSB_ERROR code on failure

void libusb_exit ( struct libusb_context * ctx )
Deinitialize libusb.
Should be called after closing all open devices and before your
application terminates.
Parameters:
ctx the context to deinitialize, or NULL for the default context

> If there is no need in the application to use a specific context
> (using a context is very important if writing a library that uses
> libusb, but is not always needed when writing an application) then
> pass NULL to libusb_init() and libusb_exit(), and then libusb will
> use the default context.
>

Maybe this can be added to the documentation especially for
users coming from libusb-0.1.

--
Xiaofan

------------------------------------------------------------------------------
For Developers, A Lot Can Happen In A Second.
Boundary is the first to Know...and Tell You.
Monitor Your Applications in Ultra-Fine Resolution. Try it FREE!
http://p.sf.net/sfu/Boundary-d2dvs2
_______________________________________________
Libusb-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/libusb-devel
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: "memory leak" debug message after libusb_close, and upon application exit.

Peter Stuge
Xiaofan Chen wrote:
> > If there is no need in the application to use a specific context
> > (using a context is very important if writing a library that uses
> > libusb, but is not always needed when writing an application) then
> > pass NULL to libusb_init() and libusb_exit(), and then libusb will
> > use the default context.
>
> Maybe this can be added to the documentation especially for
> users coming from libusb-0.1.

There is actually already some documentation about this. Is
http://libusb.sourceforge.net/api-1.0/contexts.html
OK as-is, or can you suggest some improvements?


//Peter

------------------------------------------------------------------------------
For Developers, A Lot Can Happen In A Second.
Boundary is the first to Know...and Tell You.
Monitor Your Applications in Ultra-Fine Resolution. Try it FREE!
http://p.sf.net/sfu/Boundary-d2dvs2
_______________________________________________
libusb-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/libusb-devel
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: "memory leak" debug message after libusb_close, and upon application exit.

Xiaofan Chen
On Thu, Apr 19, 2012 at 1:26 PM, Peter Stuge <[hidden email]> wrote:

> Xiaofan Chen wrote:
>> > If there is no need in the application to use a specific context
>> > (using a context is very important if writing a library that uses
>> > libusb, but is not always needed when writing an application) then
>> > pass NULL to libusb_init() and libusb_exit(), and then libusb will
>> > use the default context.
>>
>> Maybe this can be added to the documentation especially for
>> users coming from libusb-0.1.
>
> There is actually already some documentation about this. Is
> http://libusb.sourceforge.net/api-1.0/contexts.html
> OK as-is, or can you suggest some improvements?

Ah I see. It is okay as is. Daniel has really done a great job
for libusb-1.0.


--
Xiaofan

------------------------------------------------------------------------------
For Developers, A Lot Can Happen In A Second.
Boundary is the first to Know...and Tell You.
Monitor Your Applications in Ultra-Fine Resolution. Try it FREE!
http://p.sf.net/sfu/Boundary-d2dvs2
_______________________________________________
libusb-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/libusb-devel
Loading...