none
SerialPort => SerialStream => Memory leak due to _IOCompletionCallback, Overlapped, SerialStreamAsyncResult and OverlappedData instances RRS feed

  • Question

  • We are having memory leak due to the WaitForCommEvent loop in SerialStream of SerialPort.

    There are (2098037 instances) of _IOCompletionCallback, Overlapped, SerialStreamAsyncResult and OverlappedData data structures on the heap. We suspect the while loop in WaitForCommEvent is causing this leak.

    We suspect WaitForCommEvent because in all the leaked instances of SerialStreamAsyncResult, the _waitHandle object points to the same Handle as waitCommEventWaitHandle of EventLoopRunner. Hence we think this part of the code creates this SerialStreamAsyncResult.

    asyncResult = new SerialStreamAsyncResult();
    asyncResult._userCallback = null;
    asyncResult._userStateObject = null;
    asyncResult._isWrite = false;

    // we're going to use _numBytes for something different in this loop.  In this case, both
    // freeNativeOverlappedCallback and this thread will decrement that value.  Whichever one decrements it
    // to zero will be the one to free the native overlapped.  This guarantees the overlapped gets freed
    // after both the callback and GetOverlappedResult have had a chance to use it.
    asyncResult._numBytes = 2;
    asyncResult._waitHandle = waitCommEventWaitHandle;

    waitCommEventWaitHandle.Reset();
    Overlapped overlapped = new Overlapped(0, 0, waitCommEventWaitHandle.SafeWaitHandle.DangerousGetHandle(), asyncResult);
    // Pack the Overlapped class, and store it in the async result
    intOverlapped = overlapped.Pack(freeNativeOverlappedCallback, null);

    We cannot determine why this loop runs forever and creates a lot of objects AsyncPinned and not cleaned up. Is this a bug in SerialStream code and how should we fix this?

    .net Framework version used: 3.5

    Friday, February 6, 2015 3:24 AM

Answers

  • Hello Joseph,

    >>We cannot determine why this loop runs forever and creates a lot of objects AsyncPinned and not cleaned up

    Since we do not know how you construct the loop, it is hard to say what causes the loop runs forever, here is a blog which shows that we cannot debug the .NET source code:

    http://blogs.msdn.com/b/dotnet/archive/2014/02/24/a-new-look-for-net-reference-source.aspx

    If the loop condition is determined in .NET source code, you now could check the cause reason.

    Also, here is a magazine from the Microsoft which discusses about memory topic which I think might be helpful:

    Identify And Prevent Memory Leaks In Managed Code

    >>Is this a bug in SerialStream code and how should we fix this?

    If it is an outstanding issue not yet reported, please do report it on the Connect website at http://connect.microsoft.com, which allows you to give feedback directly to the product group.This approach also allows you to track the status and response on the bug once it is triaged and investigated.

    >>.net Framework version used: 3.5

    If possible, I do suggest that you could have a try with other versions as 4.0/4.5 to see if they could work for you.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, February 6, 2015 7:38 AM
    Moderator