#31 Safe handle has been closed Exception


I'm getting the above exception with 2.2.8 (from UsbTransfer finalizer called from the GC); it looks as though it's caused by my UsbEndpointReader's TransferContext not being disposed. I've gone through the code and it seems like it's down to the way IDisposable has been implemented on some of the classes (so this may happen elsewhere). In your Finalizer you shouldn't access any members as, and is the case here (OverlappedTransferContext accesses UsbDevice.Handle (via OverlappedTransferContext->Wait()/UsbEndpointBase.Handle)) the member may already be disposed (you can't determine the order objects will be finalized).

Usually the pattern would be :

class x : IDisposable
private bool _disposed;


protected virtual void Dispose(bool disposing)
if(disposing && !_disposed)
_diposed = true;
/// do stuff here

void Dispose()

Might just be quicker to dispose the context in UsbEndpointBase, but working in the finalizer is bound to cause problems sooner or later.


  • Travis Robinson

    Travis Robinson - 2010-12-02

    Thanks for pointing this out. This was actually reported last week; I came to the same conclusion; that there were some issues in they way IDisposable was implemented in a few of the classes. :)

    The original report is here:

    Your tracker has more info, so I will keep them both. It should also notify when this issue is resolved.

    Thanks Again,

  • Travis Robinson

    Travis Robinson - 2010-12-02
    • milestone: --> v2.2.8
    • priority: 5 --> 7
    • status: open --> open-duplicate
  • Chuck Gannon

    Chuck Gannon - 2014-04-15

    I am having a similar issue with our application.

    I have tried using the r113 release as well. I incorporated the code into our solution. If I disconnect our USB device and quickly reconnect, I get get exception in OverlappedTransferContext.cs in method Wait at line 97.

    The EndPointBase throwing the exception is always LibUsbDotNet.UsbEndpointWriter. The endpoint writer indicates it is already disposed and the EndpointBase.Handle indicates it is closed.

    If I dummy up the code and add a check for EndpointBase.Handle.IsClosed before calling GetOverlappedResult I can stop it from throwing. I'm guessing this is not the optimal solution. Also not sure if we are correctly closing everything when we detect disconnect (although our code follows the examples in the project).

    Any help you can provide would be appreciated. Thanks


Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

No, thanks